zoukankan      html  css  js  c++  java
  • Linq to Xml示例

    Xml文件:

    <?xml version="1.0"?>
    <Software xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <Databases>
        <Database>
          <Name>Data</Name>
          <Password />
          <InitializeCommand />
          <Tables>
            <Table>
              <Name>DataTable</Name>
              <Columns>
                <Column>
                  <Name>ID</Name>
                  <Type>Int32</Type>
                  <Comment />
                </Column>
                <Column>
                  <Name>Name</Name>
                  <Type>String</Type>
                  <Comment />
                </Column>
                <Column>
                  <Name>Meaning</Name>
                  <Type>String</Type>
                  <Comment />
                </Column>
                <Column>
                  <Name>Important</Name>
                  <Type>Int32</Type>
                  <Comment />
                </Column>
              </Columns>
              <Comment />
            </Table>
          </Tables>
        </Database>
      </Databases>
      <DALNameSpace>WordsHelper.DAL</DALNameSpace>
      <ModelNameSpace>WordsHelper.Model</ModelNameSpace>
      <OutputDirectory>..Library</OutputDirectory>
    </Software>

    查询代码:

    var cust = from c in XElement.Load("Last.xml")
                    .Elements("Databases").Elements("Database")
                    .Elements("Tables").Elements("Table")
                    .Elements("Columns").Elements("Column")
                where c.Element("Name").Value == "Name"
                select c;
    var x = cust.ToArray()[0].Element("Type").Value;

     简化的代码:

    var x = XElement.Load("Last.xml")
        .Elements("Databases")
        .Elements("Database")
        .Elements("Tables")
        .Elements("Table")
        .Elements("Columns")
        .Elements("Column")
        .First(c => c.Element("Name").Value == "Name")
        .Element("Type")
        .Value;
  • 相关阅读:
    WSGI学习系列WSME
    Murano Weekly Meeting 2015.08.11
    Trace Logging Level
    OpenStack Weekly Rank 2015.08.10
    markdown语法测试集合
    css-定位
    html图像、绝对路径和相对路径,链接
    html块、含样式的标签
    html标题、段落、换行与字符实体
    html概述和基本结构
  • 原文地址:https://www.cnblogs.com/yao2yao4/p/3207856.html
Copyright © 2011-2022 走看看