开发者

select XElements with xmlns

开发者 https://www.devze.com 2023-03-05 16:24 出处:网络
How do you select an element with xmlns specified? I n开发者_Python百科eed to select Include/Fragment element. I\'ve tried addinghttp://schemas.microsoft.com/wix/2006/wi before element names, but that

How do you select an element with xmlns specified? I n开发者_Python百科eed 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");


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!


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号