我有xml以下。如果我有FolderName,我可以在数组中获取FileTypeId,EngName,JpnName。例如,如果我有Test2,我想使用XDocument类在字符串数组中获取2,EngName2,JpnName2。
<Row>
<FolderName>Test1</FolderName>
<FileTypeId>1</FileTypeId>
<EngName>EngName1</EngName>
<JpnName>JpnName1</JpnName>
</Row>
<Row>
<FolderName>Test2</FolderName>
<FileTypeId>2</FileTypeId>
<EngName>EngName2</EngName>
<JpnName>JpnName2</JpnName>
</Row>
<Row>
<FolderName>Test3</FolderName>
<FileTypeId>3</FileTypeId>
<EngName>EngName3</EngName>
<JpnName>JpnName3</JpnName>
</Row>
答案 0 :(得分:0)
你需要主根,
var doc = XDocument.Load("data.xml");
<Rows>
<Row>
<FolderName>Test1</FolderName>
<FileTypeId>1</FileTypeId>
<EngName>EngName1</EngName>
<JpnName>JpnName1</JpnName>
</Row>
<Row>
<FolderName>Test2</FolderName>
<FileTypeId>2</FileTypeId>
<EngName>EngName2</EngName>
<JpnName>JpnName2</JpnName>
</Row>
<Row>
<FolderName>Test3</FolderName>
<FileTypeId>3</FileTypeId>
<EngName>EngName3</EngName>
<JpnName>JpnName3</JpnName>
</Row>
</Rows>
答案 1 :(得分:0)
尝试使用LINQ:
XDocument xdoc = XDocument.Load("input.xml");
string yourCondition = "Test2";
var query = from elem in xdoc.Root.Elements("Row")
where elem.Element("FolderName").Value == yourCondition
select new
{
FileTypeId = elem.Element("FileTypeId").Value,
EngName = elem.Element("EngName").Value,
JpnName = elem.Element("JpnName").Value
};
if (query.Count() > 0)
{
var result = query.First();
// You can access the fields through this:
string engName = result.EngName;
string fileTypeId = result.FileTypeId;
string jpnName = result.JpnName;
}
正如“snn brn”指出的那样,你的xml中需要一个Root元素。