XML文档深入

发布于 2024-12-08 10:46:07 字数 913 浏览 2 评论 0原文

现在我的代码与此类似(实际上更深一层)。如果公司名称等于公司的节点,则从中创建一个节点列表(因为我需要用所有详细信息填充下拉框)---不为该项目使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等风也等你 2024-12-15 10:46:07

看一下 XPathXPathNavigator 类,它为您提供了一种 XML 查询语言

,或者您可以根据需要使用 Linq to XML,但这取决于 .net您正在使用的框架。

Have a look at XPath and the XPathNavigator class, it provides you with a query language for XML

Or you could use Linq to XML if you want but that depends on the .net framework you're using.

酒浓于脸红 2024-12-15 10:46:07

这个怎么样?

[XmlRoot("companies")]
public class Root
{
    [XmlElement("company")]    
    public company[] companies;
}

public class company
{
    [XmlAttribute("id")]
    public string id;
    public string code;
    public string detail;
}


XmlSerializer xml = new XmlSerializer(typeof(Root));
Root r = (Root)xml.Deserialize(new StringReader(xmlstr));

How about this?

[XmlRoot("companies")]
public class Root
{
    [XmlElement("company")]    
    public company[] companies;
}

public class company
{
    [XmlAttribute("id")]
    public string id;
    public string code;
    public string detail;
}


XmlSerializer xml = new XmlSerializer(typeof(Root));
Root r = (Root)xml.Deserialize(new StringReader(xmlstr));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文