0

How do you select an element with xmlns specified? I need to select Include/Fragment element. I've tried adding http://schemas.microsoft.com/wix/2006/wi before element names, but that doesn't work. In XmlDocument there was NamespaceManager functionality, but I don't see same stuff in XDocument. So how do I select an element with xmlns?

<Include xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Fragment/>
</Include>

I've tried:

IEnumerable<XElement> Fragments = d.Element("Include").Elements("Fragment");

and

const string xmlns="http://schemas.microsoft.com/wix/2006/wi/";
IEnumerable<XElement> Fragments = d.Element(xmlns+"Include").Elements(xmlns+"Fragment");
pnuts
  • 58,317
  • 11
  • 87
  • 139
Bogdan Verbenets
  • 25,686
  • 13
  • 66
  • 119

2 Answers2

2

You just need to make your xmlns variable a XNamespace (instead of just a string):

XNamespace xmlns = "http://schemas.microsoft.com/wix/2006/wi";

IEnumerable<XElement> Fragments = doc.Element(xmlns + "Include").Elements(xmlns + "Fragment");

then it should work just fine!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
0
XElement yourfile = XElement.Load("yourfile.xml");
IEnumerable<XElement> address =
    from el in yourfile.Elements("Include")
    where (string)el.Attribute("XElement") !=null
    select el;

I tried to implement the code here: http://msdn.microsoft.com/en-us/library/bb675197.aspx It also converts it to a list. Hope that helps

Kuzgun
  • 4,649
  • 4
  • 34
  • 48