views:

15

answers:

1

Hi All,

I need to write a Linq query to get XElements based on Attribute Name.

The XMl is not a structured one.. The attribute may there at the top of some XML node or any where in the xml ?

Well actually it is the word document the xml file document.xml there will be places it uses the r:Id ,Now i need to get all the Elements which contain this attribute !

Below is the XML

> <w:p w:rsidR="00302209"
> w:rsidRDefault="005C326E"
> w:rsidP="00302209">
> - <w:pPr>   <w:framePr w:w="3186" w:h="1701" w:hSpace="142"
> w:wrap="around" w:vAnchor="page"
> w:hAnchor="page" w:x="8688"
> w:y="11449" />    <w:spacing
> w:line="240" w:lineRule="exact" /> 
> - <w:rPr>   <w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman"
> />    <w:szCs w:val="24" />   
> </w:rPr>   </w:pPr>
> - <w:hyperlink r:id="rId6" w:history="1">
> - <w:r w:rsidR="00302209">
> - <w:rPr>   <w:rStyle w:val="Hyperlink" />    <w:rFonts
> w:ascii="Times New Roman"
> w:hAnsi="Times New Roman" />   
> <w:szCs w:val="24" />    </w:rPr>  
> <w:t>dd.com</w:t>    </w:r>  
> </w:hyperlink>   </w:p> <w:sectPr
> w:rsidR="001D3CA8"
> w:rsidRPr="00E275EA"
> w:rsidSect="00522450">  
> <w:headerReference w:type="even"
> r:id="rId7" />    <w:headerReference
> w:type="default" r:id="rId8" />   
> <w:footerReference w:type="even"
> r:id="rId9" />    <w:footerReference
> w:type="default" r:id="rId10" />   
> <w:headerReference w:type="first"
> r:id="rId11" />    <w:footerReference
> w:type="first" r:id="rId12" />   
> <w:pgSz w:w="11907" w:h="16840"
> w:code="9" />    <w:pgMar w:top="1701"
> w:right="3572" w:bottom="510"
> w:left="1134" w:header="567"
> w:footer="238" w:gutter="0" />   
> <w:cols w:space="708" />    <w:titlePg
> />    </w:sectPr>

Regards Francis P.

A: 

Sure, it's easy:

var elements = doc.Descendants().Where(x => x.Attribute("Foo") != null);

Let me know if that's not what you were after...

Jon Skeet
XNamespace relationShipDefns = "http://schemas.openxmlformats.org/package/2006/relationships";XName name = XName.Get("Id", relationShipDefns.NamespaceName);IEnumerable<XElement> eles = contentRoot.Descendants().Where(x => x.Attribute(name) != null);
francis
This is my code i get the elements count to 0!
francis
@francis: Please post the XML and a sample test program in a format we can just and paste - then it should be easy to sort out. Note, however, that you're using "Id" as the attribute name whereas it's "id" in the sample data you provided... try that first!
Jon Skeet
Thanks Jon,I solved it !! The XNamespace i had taken was a wrong one I was using this Namesspace used "http://schemas.openxmlformats.org/package/2006/relationships"I then corrected it to "http://schemas.openxmlformats.org/officeDocument/2006/relationships" .Thanks for you help :)
francis