XML文档深入
现在我的代码与此类似(实际上更深一层)。如果公司名称等于公司的节点,则从中创建一个节点列表(因为我需要用所有详细信息填充下拉框)---不为该项目使用 3.5 :(
XmlNodeList elemList = xmlDoc.GetElementsByTagName("company");
foreach (XmlNode node in elemList)
{
if (node.Attributes[0].Value == company)
{
foreach (XmlNode child in node.ChildNodes)
{
foreach (XmlNode detail in child.ChildNodes)
{
ddlCodes.Items.Add(detail.Value.ToString());
}
}
}
}
不太喜欢所有这些foreach
语句,只是想知道是否有更简洁的方法,这是我的 xml 的样子。
<companies>
<company id="company1">
<code>12</code>
<detail>detail of 12 code</detail>
</company>
<company id="company2">
<code>15</code>
<detail>detail of 15 code</detail>
</company>
</companies>
Right now I have my code similar to this (actually one level deeper). If the company name is equal to the node of company then create a node list from it (as I need to populate a drop down box with all of the details) ---Not using 3.5 for this project :(
XmlNodeList elemList = xmlDoc.GetElementsByTagName("company");
foreach (XmlNode node in elemList)
{
if (node.Attributes[0].Value == company)
{
foreach (XmlNode child in node.ChildNodes)
{
foreach (XmlNode detail in child.ChildNodes)
{
ddlCodes.Items.Add(detail.Value.ToString());
}
}
}
}
Not really liking all those foreach
statements, just wondering if there is a cleaner way. Here is how my xml looks like
<companies>
<company id="company1">
<code>12</code>
<detail>detail of 12 code</detail>
</company>
<company id="company2">
<code>15</code>
<detail>detail of 15 code</detail>
</company>
</companies>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看一下
XPath
和XPathNavigator
类,它为您提供了一种 XML 查询语言,或者您可以根据需要使用 Linq to XML,但这取决于 .net您正在使用的框架。
Have a look at
XPath
and theXPathNavigator
class, it provides you with a query language for XMLOr you could use Linq to XML if you want but that depends on the .net framework you're using.
这个怎么样?
How about this?