2017-10-30 22 views
0

xmlからいくつかのデータを読み込もうとしています。以下は私のXML構造体です。get xpathの問題

<Configuration> 
<node1></node1> 
<unity 
xmlns=xmlns="http://schemas.microsoft.com/practices/2010/unity"> 
<namesapce name="somename"/> . . . 
</unity> 
</Configuration> 

私は

のxpath以下
var nodelist=doc.SelectNodes("/configuration/unity[@xmlns='http://schemas.microsoft.com/practices/2010/unity']"); 

を使用してノードを読み取るために、 "団結" を試してみました。しかし、それはnullを返します。私のコードで何が間違っていますか?

+0

をさて、あなたは= 'のxmlns =ののxmlnsを持っている "... "'ただ一つ 'のxmlns =" ..." があるはずです'。それが疑問であると仮定すると、 'xmlns =" http://schemas.microsoft.com/practices/2010/unity "は[XML名前空間宣言](https://en.wikipedia.org/wiki/)です。 XML_namespace#Namespace_declaration)ので、[デフォルトネームスペースを使用するノードの選択方法](https://stackoverflow.com/q/14370989)と[C#でのデフォルトネームスペースでのXpathの使用](https ://stackoverflow.com/q/585812)。 – dbc

答えて

0

これを試してください:あなたのXMLが壊れている(名前空間を無視して)

string xmlString = "<Configuration><node1></node1><unity xmlns=\"http://schemas.microsoft.com/practices/2010/unity\"><namesapce name=\"somename\"/></unity></Configuration>"; 
    XmlDocument xd = new XmlDocument(); 
    xd.LoadXml(xmlString); 
    var nodes = xd.SelectNodes("//*[local-name()='unity']"); 

    Console.WriteLine(nodes.Count);