我想填充这个xml
<head xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/">
<arm>
<fingers>
<middle>1</middle>
<middle>2</middle>
</fingers>
<fingers>
<middle>3</middle>
<middle>4</middle>
</fingers>
</arm>
</head>
我希望文件在列表框中显示
答案 0 :(得分:0)
如果要在ListBox中显示中间标记内容列表,则无法以这种方式执行此操作:
var xml = @"<head xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://tempuri.org/"">
<arm>
<fingers>
<middle>1</middle>
<middle>2</middle>
</fingers>
<fingers>
<middle>3</middle>
<middle>4</middle>
</fingers>
</arm>
</head>";
var doc = XDocument.Parse(xml);
XNamespace xmlNamespace = "http://tempuri.org/";
var middles = from middle in doc.Descendants(xmlNamespace + "middle") select middle.Value;
MyListBox.ItemsSource = middles;
上面的代码使用linq-to-xml仅查询<middle>
标记内的值。然后在MyListBox
。