【问题标题】:.NET search sub-elements in xml.NET 搜索 xml 中的子元素
【发布时间】:2019-11-20 02:42:13
【问题描述】:

我正在尝试按照官方 microsoft dotNet api 中的说明使用 Linq 将 XML 文件的内容加载到自定义类型列表中: https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.find?view=netframework-4.8

我的 xml 文件如下所示:

<directives>
    <dir directive="Question" response="Response"></dir>
    <dir directive="Q2" response="Response2"></dir>
    <dir directive="Q3" response="Response3"></dir>
</directives>

并且导入代码与上面链接的示例几乎相同。

public static void ImportDirectives()
{
    // Create XML elements from a source file.
    XElement xTree = XElement.Load(AppDomain.CurrentDomain.BaseDirectory + "directives.xml");

    // Create an enumerable collection of the elements.
    IEnumerable<XElement> elements = xTree.Elements();

    // Evaluate each element and set set values in the book object.
    foreach (XElement el in elements)
    {
        Directive dir = new Directive();
        dir.directive = el.Attribute("directive").Value;
        IEnumerable<XElement> props = el.Elements();
        foreach (XElement p in props)
        {
            if (p.Name.ToString().ToLower() == "response")
            {
                dir.response = p.Value;
            }
        }
        Dir.Add(dir);
    }
}

如果我删除根元素,代码可以正常工作,但如果我添加一个,则只将根元素添加到我的列表中。 我宁愿有一个根元素只是为了让我的 XML 看起来正确。 如何使用此代码访问根元素中的元素?

【问题讨论】:

    标签: c# .net xml list


    【解决方案1】:

    当你添加 root 然后xml 喜欢

    <Root>
      <directives>
        <dir directive="Question" response="Response"></dir>
        <dir directive="Q2" response="Response2"></dir>
        <dir directive="Q3" response="Response3"></dir>
      </directives>
    </Root>
    

    当你先获取 Elements() 然后

    <directives>
        <dir directive="Question" response="Response"></dir>
        <dir directive="Q2" response="Response2"></dir>
        <dir directive="Q3" response="Response3"></dir>
      </directives>
    

    再次获取 Elements() 然后你会得到 Node

    <dir directive="Question" response="Response"></dir>
    

    然后你访问属性和值

          public static void ImportDirectives()
            {
                // Create XML elements from a source file.
                XElement xTree = XElement.Load(AppDomain.CurrentDomain.BaseDirectory + "directives.xml");
    
                // Create an enumerable collection of the elements.
                IEnumerable<XElement> elements = xTree.Elements();
    
                // Evaluate each element and set set values in the book object.
                foreach (XElement el in elements.Elements())
                {
                    Directive dir = new Directive();
                    dir.directive = el.Attribute("directive").Value;
                    IEnumerable<XElement> props = el.Elements();
                    foreach (XElement p in props)
                    {
                        if (p.Name.ToString().ToLower() == "response")
                        {
                            dir.response = p.Value;
                        }
                    }
                    Dir.Add(dir);
                }
            }
    

    【讨论】:

      【解决方案2】:

      如果您确定您的 XML Schema 将被修复,您可以直接访问 XML 元素的属性值。

      我附上了一个示例代码。

      public static void ImportDirectives()
          {
              string fileName = AppDomain.CurrentDomain.BaseDirectory + "directives.xml";
              // Create XML elements from a source file.
              XElement xTree = XElement.Load(fileName);
      
              // Create an enumerable collection of the elements.
              IEnumerable<XElement> elements = xTree.Elements();
      
              // Evaluate each element and set set values in the book object.
              foreach (XElement el in elements)
              {
                  string directive = el.Attribute("directive").Value;
                  string response = el.Attribute("response").Value;
      
                  Console.WriteLine(directive + ":" + response);
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-06-18
        • 2023-01-11
        • 2013-03-26
        • 1970-01-01
        • 1970-01-01
        • 2013-04-24
        • 1970-01-01
        相关资源
        最近更新 更多