更好的读取 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 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>
...
,我想读取 sub_child 属性到类
class SubChild {
string a1;
string a2;
}
我需要 child_1 和 child_2 的两个 SubChild 对象列表以及 sub_child_1 和 sub_child_2 的两个 SubChild 对象
现在我使用 linq to xml ,我的代码是,
// 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();
但它看起来很难看,我想请问有没有更明确的方法来做到这一点?
I have xml file
<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>
...
and I want to read sub_child attributes to classes
class SubChild {
string a1;
string a2;
}
I need two lists of SubChild objects for child_1 and child_2 and two SubChild objects for sub_child_1 and sub_child_2
Now I use linq to xml and my code is
// 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();
but it looks ugly and I would like to ask is there more clear way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
.Element()
和.Elements()
指定“Path”,从而消除where
lambda。You can specify a "Path" using
.Element()
and.Elements()
, eliminating thewhere
lambdas.更进一步,您可以创建一个包含元素的所有子子元素的字典。这将允许您按名称直接访问每个元素的
SubChild
。To go a couple of steps further, you could create a dictionary containing all the subchildren of your elements. This will allow you direct access to the
SubChild
's of each element by name.您真的必须使用 linq to xml 吗?
Do you really have to use linq to xml?