0

I try to get the value of "firstName" of the following XML Code:

<?xml version="1.0" encoding="utf-8"?>
<Userlist xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <user username="Max.mix" id="464" adult="true">
    <registeredBy>Internet</registeredBy>
    <firstName>Max</firstName>
    <birthday>15031975</birthday>
  </user>
  <user username="Sus.lab" id="125" adult="false">
    <registeredBy>Phone</registeredBy>
    <firstName>Susanne</firstName>
    <birthday>03112007</birthday>
  </user>
</Userlist >

But I get everytime a empty (null) value back with this method:

public List<List> GetFirstName()
        {
            const string filename = @"C:\XML.xml";

            string text = File.ReadAllText(filename);
            XDocument doc = XDocument.Parse(text);

            List<List> firstnameList = doc.Root.Elements().Select(x => new List
            {
                firstName = (string)x.Element("firstName"),
            }).ToList();

            return firstnameList ;

        } 

public class List
    {
        public string firstName{ get; set; }

    }

Where I have the Problem?

Thank you.

bmw_58
  • 119
  • 1
  • 13
  • The problem is that `doc.Element()` only returns the top level, i.e. "user". You want to search for "firstName" at *ANY* level. One alternative might be to try `doc.Descendants("firstName")`: https://stackoverflow.com/questions/566167/query-an-xdocument-for-elements-by-name-at-any-depth – FoggyDay May 19 '20 at 20:22
  • As a general debugging tip: You should break apart your one-liner statement into multiple statements then run a `foreach` loop instead of `Select`. This way you can place breakpoints and see what the values are at each step. Once you've figured out what the structure is and where you've gone wrong you can re-condense it (or not). – MikeH May 19 '20 at 20:24

2 Answers2

0

XElement.Elements only returns the top-level elements, not all nested elements.

See docs: https://learn.microsoft.com/en-us/dotnet/api/system.xml.linq.xcontainer.elements?view=netcore-3.1

I think you may want GetDescendantNodes() https://learn.microsoft.com/en-us/dotnet/api/system.xml.linq.xcontainer.descendantnodes?view=netcore-3.1#System_Xml_Linq_XContainer_DescendantNodes

JamesFaix
  • 8,050
  • 9
  • 37
  • 73
0
    public List<string> GetFirstNames()
    {
        const string filename = @"C:\XML.xml";

        string text = File.ReadAllText(filename);
        XDocument doc = XDocument.Parse(text);
        List<string> firstnameList =
            doc.Root.Elements().Select(x => x.Element("firstName").Value).ToList();

        return firstnameList;
    }
alec
  • 396
  • 1
  • 6
  • What I get is an System.NullReferenceException with this method. – bmw_58 May 19 '20 at 20:36
  • It works with the test file you supplied. You can add the necessary null check to the select if all the "user" elements in your real file do not contain a "firstName" element. – alec May 19 '20 at 20:39
  • Can you give me a sample for this: List firstnameList = doc.Root.Elements().Select(x => new List { firstName = (string)x.Element("firstName"), }).ToList(); Because I have everytime a strange value. So I mean not the correct value. – bmw_58 May 19 '20 at 22:06