2011-05-11 42 views
0

如何選擇指定了xmlns的元素?我需要選擇包含/片段元素。我試過在元素名稱前添加http://schemas.microsoft.com/wix/2006/wi,但這不起作用。在XmlDocument中有NamespaceManager的功能,但我在XDocument中看不到相同的東西。那麼如何用xmlns選擇一個元素?使用xmlns選擇XElements

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

我已經試過:

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

const string xmlns="http://schemas.microsoft.com/wix/2006/wi/"; 
IEnumerable<XElement> Fragments = d.Element(xmlns+"Include").Elements(xmlns+"Fragment"); 
+0

我假設這是一個Linq-to-xml問題(因此是retag)。你可以包括你已經嘗試過什麼,不起作用嗎? – 2011-05-11 15:13:27

+0

@Conrad Frix我更新了我的問題。 – 2011-05-11 15:15:31

回答

2

你只需要讓你的xmlns變量XNamespace(而不是隻是一個字符串):

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

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

然後我噸應該工作得很好!

0
XElement yourfile = XElement.Load("yourfile.xml"); 
IEnumerable<XElement> address = 
    from el in yourfile.Elements("Include") 
    where (string)el.Attribute("XElement") !=null 
    select el; 

我試圖執行此代碼:http://msdn.microsoft.com/en-us/library/bb675197.aspx 它也把它轉換成一個列表。希望有幫助