更好的读取 xml 的方法

发布于 2024-11-19 07:29:17 字数 1875 浏览 3 评论 0原文

我有 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 技术交流群。

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

发布评论

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

评论(3

与往事干杯 2024-11-26 07:29:17

您可以使用 .Element().Elements() 指定“Path”,从而消除 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,
          };

You can specify a "Path" using .Element() and .Elements(), eliminating the where lambdas.

 // 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,
          };
怕倦 2024-11-26 07:29:17

更进一步,您可以创建一个包含元素的所有子子元素的字典。这将允许您按名称直接访问每个元素的 SubChild

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"

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.

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"
爱你是孤单的心事 2024-11-26 07:29:17

您真的必须使用 linq to 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 };
       }
   }
}

Do you really have to use linq to 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 };
       }
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文