简体   繁体   中英

.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

My xml file looks like this:

<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. 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.

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>
  <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

<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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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