-1

如何从XML下面读取值是XML我正在阅读的我想读取 ID 和答案的值

<d:Answer xmlns:d="http://www.test.com/" d:title="abcd">
  <d:question id="2.1" answer="test"  />
  <d:question id="2.2" answer="test"  />
  <d:question id="2.3" answer="Yes"  />
</d:Answer>

我正在使用 Xelement 读取值但无法找到

4

1 回答 1

1

试试这个:

var xml = @"<d:Answer xmlns:d=""http://www.test.com"" d:title=""abcd"">
  <d:question id=""2.1"" answer=""test""  />
  <d:question id=""2.2"" answer=""test""  />
  <d:question id=""2.3"" answer=""Yes""  />
</d:Answer>";

XNamespace ns = "http://www.test.com";
var doc = XDocument.Parse(xml);
var question = doc.Descendants(ns + "question")
                  .FirstOrDefault(x => (string)x.Attribute("id") == "2.1");
于 2012-12-11T11:17:52.153 回答