ankur
ankur

Reputation: 4733

get attribute value from xsd node using LINQ

I have below xsd file .

<?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>

I want to find out value of name attribute of xs:element tag.

I have used the below code it keeps throwing error or it is 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();

both of them throws error , please suggest to get the values.

Upvotes: 0

Views: 623

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167571

Use

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

and

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

gives you all name attribute values of all xs:element elements. If you only want the top level elements then use

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

Upvotes: 2

Related Questions