繁体   English   中英

.NET 搜索 xml 中的子元素

[英].NET search sub-elements in xml

I'm trying to load contents of a XML file into a list of custom types using Linq following the instructions in the official microsoft dotNet api: https://docs.microsoft.com/en-us/dotnet/api/system.collections .generic.list-1.find?view=network-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 看起来合适。 如何使用此代码访问根元素中的元素?

如果您确定您的 XML 架构将得到修复,您可以直接访问 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);
        }
    }

当你添加 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>

再次 fetch 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);
            }
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM