tags:

views:

14

answers:

1

Hi

I have got below XML document

 <Documents Databasename="Type1">
<Document>
  <Documentnumber>xxxxxx</Documentnumber>
  <Documentname>xxxxxx</Documentname>
  <DocumentExtension>xxxxxx</DocumentExtension>
  <ImportAction>xxxxxx</ImportAction>
  <Authorname>xxxxxx</Authorname>
  <Practicegroup>xxxxxx</Practicegroup>
  <Createddate>xxxxxx</Createddate>
  <Createdby>xxxxxx</Createdby>
  <Modifieddate>xxxxxx</Modifieddate>
  <Modifiedby>xxxxxx</Modifiedby>
  <Lastaccessddate>xxxxxx</Lastaccessddate>
  <Comments />
  <Sector>Unknown</Sector>
  <Source>Unknown</Source>
  <Quality>Unknown</Quality>
  <KnowHowContentType>Unknown</KnowHowContentType>
  <ActualAutors />
  <SubmittedBy />
  <NumberofAccess>0</NumberofAccess>
  <AreasofLaw />
</Document>
<Document>
  <Documentnumber>xxxxxx</Documentnumber>
  <Documentname>xxxxxx</Documentname>
  <DocumentExtension>xxxxxx</DocumentExtension>
  <ImportAction>xxxxxx</ImportAction>
  <Authorname>xxxxxx</Authorname>
  <Practicegroup>xxxxxx</Practicegroup>
  <Createddate>xxxxxx</Createddate>
  <Createdby>xxxxxx</Createdby>
  <Modifieddate>xxxxxx</Modifieddate>
  <Modifiedby>xxxxxx</Modifiedby>
  <Lastaccessddate>xxxxxx</Lastaccessddate>
  <Comments />
  <Sector>Unknown</Sector>
  <Source>Unknown</Source>
  <Quality>Unknown</Quality>
  <KnowHowContentType>Unknown</KnowHowContentType>
  <ActualAutors />
  <SubmittedBy />
  <NumberofAccess>0</NumberofAccess>
  <AreasofLaw />
</Document> 

xxxxxx xxxxxx xxxxxx xxxxxx xxxxxx xxxxxx xxxxxx xxxxxx xxxxxx xxxxxx xxxxxx Unknown Unknown Unknown Unknown 0 xxxxxx xxxxxx xxxxxx xxxxxx xxxxxx xxxxxx xxxxxx xxxxxx xxxxxx xxxxxx xxxxxx Unknown Unknown Unknown Unknown 0

I want to select DocumentNumber, DocumentExtension for a given attribute Databasename. Please let me know if I explained my self.

A: 

I haven't used Linq to XML much, so this probably isn't the best way, but it seems to work:

string xml = @"
<root>
    <Documents Databasename='Type1'>
    <Document>
      <Documentnumber>1234</Documentnumber>";
      // etc..

XDocument xmlDoc = XDocument.Parse(xml);

var output = from item in xmlDoc.Descendants()
             where item.Name == "Document"
             && item.Parent.Attribute("Databasename").Value == "Type1"
             select new { Number = (string)item.Element("Documentnumber").Value,
                          Extension = item.Element("DocumentExtension").Value
             };

foreach (var item in output) {
    Console.WriteLine(item);
}

Note that I added an extra 'root' element to the XML to make this work (assuming multiple 'Documents' elements).

Grant Crofton
Perfect. Thanks Grant appreciate your help on this.