C# 更好的读取xml的方法
我有一个xml文件C# 更好的读取xml的方法,c#,linq-to-xml,C#,Linq To Xml,我有一个xml文件 <root> <child_1> <sub_child attr1="text" attr2="text" /> <sub_child attr1="text" attr2="text" /> <sub_child attr1="text" attr2="text" /> </child_1> <child_2> <sub_child
<root>
<child_1>
<sub_child attr1="text" attr2="text" />
<sub_child attr1="text" attr2="text" />
<sub_child attr1="text" attr2="text" />
</child_1>
<child_2>
<sub_child attr1="text" attr2="text" />
<sub_child attr1="text" attr2="text" />
<sub_child attr1="text" attr2="text" />
</child_2>
<child_3>
<sub_child_1 attr1="text" attr2="text" />
<sub_child_2 attr1="text" attr2="text" />
</child_3>
...
我需要为child_1和child_2列出两个SubChild对象,为sub_child_1和sub_child_2列出两个SubChild对象
现在我使用LINQtoXML,我的代码是
// List<SubChild>
var child_1 = from s in doc.Descendants("sub_child").Where(x => x.Parent.Name == "child_1")
select new SubChild {
a1 = s.Attribute("attr1").Value,
a2 = s.Attribute("attr2").Value,
};
// List<SubChild>
var child_2 = from s in doc.Descendants("sub_child").Where(x => x.Parent.Name == "child_2")
select new SubChild {
a1 = s.Attribute("attr1").Value,
a2 = s.Attribute("attr2").Value,
};
// SubChild
var sub_1 = (from s in doc.Descendants("sub_child_1")
select new SubChild {
a1 = s.Attribute("attr1").Value,
a2 = s.Attribute("attr2").Value,
}).First();
// SubChild
var sub_2 = (from s in doc.Descendants("sub_child_2")
select new SubChild {
a1 = s.Attribute("attr1").Value,
a2 = s.Attribute("attr2").Value,
}).First();
但是它看起来很难看,我想问有没有更清晰的方法来实现这一点?您可以使用.Element和.Elements指定路径,从而消除where lambda
// untested
var child_1 = from s in doc.Root.Element("child_1").Elements("sub_child")
select new SubChild {
a1 = s.Attribute("attr1").Value,
a2 = s.Attribute("attr2").Value,
};
再进一步,您可以创建一个包含元素所有子元素的字典。这将允许您通过名称直接访问每个元素的子项
Dictionary<string, List<SubChild>> dict = doc.Root.Elements().ToDictionary(
e => e.Name.ToString(),
e => e.Elements("sub_child")
.Select(s => new SubChild
{
a1 = s.Attribute("attr1").Value,
a2 = s.Attribute("attr2").Value,
}).ToList());
dict["child_1"][0] // get the first subchild of "child_1"
您真的必须使用linq转换xml吗
public IEnumerable<SubChild> GetSubChilds(XmlDocument xd)
{
foreach (XmlNode subChild in xd.SelectNodes("/root/*/*"))
{
if (subChild.Name.StartsWith("sub_child"))
{
XmlAttributeCollection atts = subChild.Attributes;
yield return new SubChild { a1 = atts["attr1"].Value, a2 = atts["attr2"].Value };
}
}
}
但这比林克好吗?请注意,您将需要其中两个参数。想象一下道路的变化。
public IEnumerable<SubChild> GetSubChilds(XmlDocument xd)
{
foreach (XmlNode subChild in xd.SelectNodes("/root/*/*"))
{
if (subChild.Name.StartsWith("sub_child"))
{
XmlAttributeCollection atts = subChild.Attributes;
yield return new SubChild { a1 = atts["attr1"].Value, a2 = atts["attr2"].Value };
}
}
}