从属性值 XML C# 中读取所有匹配项
Reading all matches from attribute value XML C#
我有一个代码,它使用 xmlreader 从 xml 文件中只检索一个值。
<recordset>
<itemidlist>
<itemid idtype = "plant">787484545</itemid>
<itemid idtype = "seed">659988222</itemid>
</itemidlist>
<itemidlist>
<itemid idtype = "plant">90327328</itemid>
<itemid idtype = "seed">099849999</itemid>
</itemidlist>
<itemidlist>
<itemid idtype = "plant">34545488</itemid>
<itemid idtype = "seed">787555444</itemid>
</itemidlist>
</recordset>
并且 C# 编码了这个条目:(s 是 xml 文件)
using (var reader = XmlReader.Create(s))
{
var nodecount = 0;
var plant = "";
var seed = "";
var typeid = false;
var typeid2 = false;
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element &&
reader.Name == "recordset")
{
nodecount++;
}
if (reader.NodeType == XmlNodeType.Element &&
reader.Name == "itemid")
{
var idtype = reader.GetAttribute("idtype");
typeid = idtype == "plant";
typeid2 = idtype == "seed";
}
while (typeid && reader.NodeType == XmlNodeType.Element &&
reader.Name == "itemid")
{
plant = reader.ReadInnerXml();
}
while (typeid2 && reader.NodeType == XmlNodeType.Element &&
reader.Name == "itemid")
{
seed = reader.ReadInnerXml();
}
}
}
当添加到 datagridview 时会发生这种情况:
只找到一条记录:
植物种子
787484545 659988222
您仅在 seed
和一个 plant
变量上设置,因此如果发现超过一个值,它将被覆盖。您如何建议 return 多个值?
有什么理由不为此使用 LINQ to XML?
var doc = XDocument.Load(s);
var plants = doc.Descendants("itemid")
.Where(x => (string) x.Attribute("idtype") == "plant")
.Select(x => x.Value);
var seeds = doc.Descendants("itemid")
.Where(x => (string) x.Attribute("idtype") == "seed")
.Select(x => x.Value);
有关工作演示,请参阅 this fiddle。
我有一个代码,它使用 xmlreader 从 xml 文件中只检索一个值。
<recordset>
<itemidlist>
<itemid idtype = "plant">787484545</itemid>
<itemid idtype = "seed">659988222</itemid>
</itemidlist>
<itemidlist>
<itemid idtype = "plant">90327328</itemid>
<itemid idtype = "seed">099849999</itemid>
</itemidlist>
<itemidlist>
<itemid idtype = "plant">34545488</itemid>
<itemid idtype = "seed">787555444</itemid>
</itemidlist>
</recordset>
并且 C# 编码了这个条目:(s 是 xml 文件)
using (var reader = XmlReader.Create(s))
{
var nodecount = 0;
var plant = "";
var seed = "";
var typeid = false;
var typeid2 = false;
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element &&
reader.Name == "recordset")
{
nodecount++;
}
if (reader.NodeType == XmlNodeType.Element &&
reader.Name == "itemid")
{
var idtype = reader.GetAttribute("idtype");
typeid = idtype == "plant";
typeid2 = idtype == "seed";
}
while (typeid && reader.NodeType == XmlNodeType.Element &&
reader.Name == "itemid")
{
plant = reader.ReadInnerXml();
}
while (typeid2 && reader.NodeType == XmlNodeType.Element &&
reader.Name == "itemid")
{
seed = reader.ReadInnerXml();
}
}
}
当添加到 datagridview 时会发生这种情况:
只找到一条记录: 植物种子 787484545 659988222
您仅在 seed
和一个 plant
变量上设置,因此如果发现超过一个值,它将被覆盖。您如何建议 return 多个值?
有什么理由不为此使用 LINQ to XML?
var doc = XDocument.Load(s);
var plants = doc.Descendants("itemid")
.Where(x => (string) x.Attribute("idtype") == "plant")
.Select(x => x.Value);
var seeds = doc.Descendants("itemid")
.Where(x => (string) x.Attribute("idtype") == "seed")
.Select(x => x.Value);
有关工作演示,请参阅 this fiddle。