使用LINQ从xsd节点获取属性值

时间:2016-09-11 14:17:07

标签: xml linq c#-4.0 xsd linq-to-xml

我在xsd文件下面。

<?xml version="1.0"?>
<xs:schema xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" elementFormDefault="qualified" targetNamespace="http://allegrodevelopment.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <!--Warning: Schema changed to permit compilation in BizTalk project.-->
  <xs:import schemaLocation=".\PriceIndexWS_development_com_PriceIndexDS.xsd" namespace="http://development.com/PriceIndexDS.xsd" />
  <xs:import schemaLocation=".\PriceIndexWS_development_com_PriceIndexDS.xsd" namespace="http://development.com/PriceIndexDS.xsd" />


 <xs:element name="SelectCriteria">
  <xs:complexType >
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="1" name="DateColumn" type="xs:string" />
      <xs:element minOccurs="1" maxOccurs="1" name="BegTime" type="xs:dateTime" />
      <xs:element minOccurs="1" maxOccurs="1" name="EndTime" type="xs:dateTime" />
      <xs:element minOccurs="1" maxOccurs="1" name="FilterByRelation" type="xs:boolean" />
      <xs:element minOccurs="1" maxOccurs="1" name="FilterDateByRelation" type="xs:boolean" />
      <xs:element minOccurs="1" maxOccurs="1" name="FilterByForeignKey" type="xs:boolean" />
      <xs:element minOccurs="0" maxOccurs="1" name="DrillColumn" type="xs:string" />
      <xs:element minOccurs="0" maxOccurs="1" name="DbCriteria" type="xs:string" />
      <xs:element minOccurs="0" maxOccurs="1" name="DbJoin" type="xs:string" />
    </xs:sequence>
  </xs:complexType>
        </xs:element>


 <xs:element name="pricevalue">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="priceindex" type="xs:string" />
              <xs:element msdata:DateTimeMode="Unspecified" name="pricedate" type="xs:dateTime" />
              <xs:element name="surrogate" type="xs:decimal" />
              <xs:element msdata:DateTimeMode="Unspecified" name="delivdate" type="xs:dateTime" />
              <xs:element name="delivtime" type="xs:decimal" />
              <xs:element name="price" type="xs:decimal" />
              <xs:element name="openprice" type="xs:decimal" />
              <xs:element name="highprice" type="xs:decimal" />
              <xs:element name="lowprice" type="xs:decimal" />
              <xs:element minOccurs="0" name="strikeprice" type="xs:decimal" />
              <xs:element minOccurs="0" name="callprice" type="xs:decimal" />
              <xs:element minOccurs="0" name="putprice" type="xs:decimal" />
              <xs:element minOccurs="0" name="source" type="xs:string" />
              <xs:element name="actualstatus" type="xs:string" />
              <xs:element minOccurs="0" name="gravity" type="xs:decimal" />
              <xs:element name="verifstatus" type="xs:boolean" />
              <xs:element minOccurs="0" name="daylightsaving" type="xs:boolean" />
              <xs:element name="creationname" type="xs:string" />
              <xs:element msdata:DateTimeMode="Unspecified" name="creationdate" type="xs:dateTime" />
              <xs:element minOccurs="0" name="revisionname" type="xs:string" />
              <xs:element msdata:DateTimeMode="Unspecified" minOccurs="0" name="revisiondate" type="xs:dateTime" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>

  </xs:schema>

我想找出name代码的xs:element属性值。

我使用了下面的代码,它不断抛出错误,或者它是null。

var data = XDocument.Load(path);

var attrList  = from item in data.Descendants().Elements()
                 where item.Attribute = "name"
         select item;

OR 
 var attrlist  = from d indata.Descendants().Elements("xs:element")
Select(d => d.Attribute("name").Value).ToList();

他们都抛出错误,请建议获取值。

1 个答案:

答案 0 :(得分:2)

使用

XNamespace xs = "http://www.w3.org/2001/XMLSchema";

List<string> values = data.Descendants(xs + "element").Attributes("name").Select(a => (string)a).ToList(); 

为您提供所有xs:element元素的所有名称属性值。如果您只想要顶级元素,请使用

List<string> values = data.Root.Elements(xs + "element").Attributes("name").Select(a => (string)a).ToList();