C# XDocument,帮助匿名抓取元素

发布于 2024-10-23 13:59:31 字数 1673 浏览 2 评论 0原文

我无法匿名获取元素。我不想直呼元素的名字。第二个 foreach 语句只是抓取整个部分,就好像它是单个元素一样。如何循环遍历所有值而不按名称调用每个值?我愿意执行 linq 语句,但从我读过的每个示例中,我不知道如何在不按名称调用每个元素的情况下使用这些语句。感谢您的帮助!

public class box
{
    public List<Person> People { get; set; } 
}

public class Person
{
    public Dictionary<string, string> data { get; set; } 
}

/*

<outer>
    <xml>
        <person>
          <data>
            <house>Big</house>
            <cell>911</cell>
            <address>NA</address>
          </data>
        </person>
        <person>
          <data>
            <house>Big</house>
            <cell>911</cell>
            <address>NA</address>
          </data>
        </person>
        <person>
          <data>
            <house>Big</house>
            <cell>911</cell>
            <address>NA</address>
          </data>
        </person>
        <person>
          <data>
            <house>Big</house>
            <cell>911</cell>
            <address>NA</address>
          </data>
        </person>
    </xml>
</outer>

*/


this.box.People = new List<Person>();

foreach (var ele in xml.Descendants("person"))
{
    Person somebody = new Person 
    {
        data = new Dictionary<string, string>(),
    };

    foreach (var temp in ele.Descendants("data")) 
    {
        somebody.data.Add(temp.Name.ToString(), temp.Value.ToString()); 
    }

    this.box.People.Add(somebody); 
}

I'm having trouble grabbing elements anonymously. I don't want to call the elements by name. The second foreach statement just grabs the entire section as if it's a single element. How do I cycle through all the values in without calling each one by name? I'm open to doing linq statements, but from every example I've read, I don't see how I can use those without calling each element by name. Thanks for any help!

public class box
{
    public List<Person> People { get; set; } 
}

public class Person
{
    public Dictionary<string, string> data { get; set; } 
}

/*

<outer>
    <xml>
        <person>
          <data>
            <house>Big</house>
            <cell>911</cell>
            <address>NA</address>
          </data>
        </person>
        <person>
          <data>
            <house>Big</house>
            <cell>911</cell>
            <address>NA</address>
          </data>
        </person>
        <person>
          <data>
            <house>Big</house>
            <cell>911</cell>
            <address>NA</address>
          </data>
        </person>
        <person>
          <data>
            <house>Big</house>
            <cell>911</cell>
            <address>NA</address>
          </data>
        </person>
    </xml>
</outer>

*/


this.box.People = new List<Person>();

foreach (var ele in xml.Descendants("person"))
{
    Person somebody = new Person 
    {
        data = new Dictionary<string, string>(),
    };

    foreach (var temp in ele.Descendants("data")) 
    {
        somebody.data.Add(temp.Name.ToString(), temp.Value.ToString()); 
    }

    this.box.People.Add(somebody); 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

不美如何 2024-10-30 13:59:31

这有效(已测试) - 只是缺少 Elements() 部分:

foreach (var temp in ele.Descendants("data").Elements()) 
{
    somebody.data.Add(temp.Name.ToString(), temp.Value); 
}

This works (tested) - was just missing the Elements() part:

foreach (var temp in ele.Descendants("data").Elements()) 
{
    somebody.data.Add(temp.Name.ToString(), temp.Value); 
}
好听的两个字的网名 2024-10-30 13:59:31

此代码遍历 xml 文档中的元素和属性。您不必为 Elements() 方法提供名称。

XDocument xmlDoc = new XDocument();
foreach (XElement element in xmlDoc.Elements()) {
    // .. Do something with the element
    foreach (XAttribute attribute in element.Attributes()) {
        // .. Do something with the attribute
    }
}

This code walks the elements and attributes within an xml document. You don't have to provide a name to the Elements() method.

XDocument xmlDoc = new XDocument();
foreach (XElement element in xmlDoc.Elements()) {
    // .. Do something with the element
    foreach (XAttribute attribute in element.Attributes()) {
        // .. Do something with the attribute
    }
}
泪眸﹌ 2024-10-30 13:59:31
foreach (var temp in ele.Descendants("data")) 
{
    foreach( var valueElem in temp.Elements() )
    {
        somebody.data.Add(valueElem.Name.LocalName, valueElem.Value); 
    }
}
foreach (var temp in ele.Descendants("data")) 
{
    foreach( var valueElem in temp.Elements() )
    {
        somebody.data.Add(valueElem.Name.LocalName, valueElem.Value); 
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文