简体   繁体   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=netframework-4.8 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

My xml file looks like this:我的 xml 文件如下所示:

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

and the importing code is pretty much the same as in the example linked above.并且导入代码与上面链接的示例几乎相同。

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);
    }
}

The code works fine if I remove the root element but only add the root element into my list if I add one.如果我删除根元素,代码工作正常,但如果我添加一个,则只将根元素添加到我的列表中。 I'd prefer having a root element just to make my XML look proper.我宁愿有一个根元素只是为了让我的 XML 看起来合适。 How would I access the elements within the root element using this code?如何使用此代码访问根元素中的元素?

If you are sure that your XML Schema is going to be fixed, you can access the attribute values of the XML elements directly.如果您确定您的 XML 架构将得到修复,您可以直接访问 XML 元素的属性值。

I have attached a sample code.我附上了一个示例代码。

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);
        }
    }

When you add root then xml like当你添加 root 然后xml

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

When you fetch first Elements() then当您获取第一个Elements()然后

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

Again fetch Elements() then you'll get Node再次 fetch Elements()然后你会得到 Node

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

Then you access attribute and values然后你访问属性和值

      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