使用c#,在以下XML中反序列化特定条目的最佳方法是什么。即我想返回元素属性“ID”的所有值,等于“0001”
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Record ID="0001">
<Random1>string1</Random1>
<Random2>string1</Random2>
<Random3>string1</Random3>
<Random4>string1</Random4>
</Record>
<Record ID="0002">
<Random1>string1</Random1>
<Random2>string1</Random2>
<Random3>string1</Random3>
<Random4>string1</Random4>
</Record>
</Data>
希望有道理,道歉是我用过的行话不正确还是个新手。
答案 0 :(得分:0)
试试这个
private List<string> GetRecordChildrenList(string xmlPath, string recordID)
{
var childNodes = new List<string>();
var doc = XDocument.Load(xmlPath);
var record = doc.Descendants("Record").FirstOrDefault(r => r.Attribute("ID").Value == recordID);
if(record != null)
childNodes = record.Descendants().Select(elm => elm.Value).ToList();
return childNodes;
}
称之为
var path = "~/App_Data/TestXML.xml";
var values = GetRecordChildrenList(path, "0001");
返回string1, string2, string3, string4