0

I am trying to read an xml file (and later import the data in to a sql data base) which contains employees names address' etc. The issue I am having is that in the xml the information for the address for the employee the node names are all the same.

<Employee>
 <EmployeeDetails>
   <Name>
     <Ttl>Mr</Ttl>
     <Fore>Baxter</Fore>
     <Fore>Loki</Fore>
     <Sur>Kelly</Sur>  
   </Name>
   <Address> 
     <Line>Woof Road</Line>
     <Line>Woof Lane</Line>
     <Line>Woofington</Line>
     <Line>London</Line> 
   </Address>   
    <BirthDate>1985-09-08</BirthDate>   
    <Gender>M</Gender>
    <PassportNumber>123756rt</PassportNumber>   
    </EmployeeDetails>
</Employee>

I all other items are fine to extract and I have tried to use Linq to iterate through each "Line" node but it always just gives be the first Line and not the others.

 var xAddreesLines = xEmployeeDetails.Descendants("Address").Select(x => new
   {
     address = (string)x.Element("Line").Value

   });
     foreach (var item in xAddreesLines)
       {
          Console.WriteLine(item.address);
       }

I need to able to when I'm importing to my sql db that address line is separate variable
eg

var addressline1 = first <line> node

var addressline2 = second <line> node etc etc.

Any advice would be most welcome.

2 Answers 2

2

This should give you the expected output:-

var xAddreesLines = xdoc.Descendants("Address")
                        .Elements("Line")
                        .Select(x => new { address = (string)x });

You need to simply fetch the Line elements present inside Address node and you can project them. Also note there is no need to call the Value property on node when you use explicit conversion.

Sign up to request clarification or add additional context in comments.

7 Comments

Key aspect here: .ElementS(...)
perfect! Thanks very much Rahul :)
@RahulSingh, It worked great, but If i want to just get one of the lines I cant get it to work. e.g., Console.WriteLine(item.address.ElementAt(0)); will just show me the first letter of each line address, any idea how I would get just the first line?
@CM99 - If you want just the first line why you are iterating over the collection? Simply say - Console.WriteLine(xAddreesLines.First().address); To avoid error use FirstOrDefault (in case there in no line node) and handle nulls properly.
@RahulSingh, thanks, I will need each line assigned to a separate variable for importing in to a sql db. the Employee table of the db has a separate column for addressline1, addressline 2 etc. (sorry quite new to linq and xml)
|
1

You can do it like this:

using System.Xml;
            .
            .
            .
            XmlDocument doc = new XmlDocument();
            doc.Load("source.xml");
            // if you have the xml in a string use doc.LoadXml(stringvar)
            XmlNamespaceManager nsmngr = new XmlNamespaceManager(doc.NameTable);
            XmlNodeList results = doc.DocumentElement.SelectNodes("child::Employee", nsmngr);
            foreach (XmlNode result in results)
            {
                XmlNode namenode = result.SelectSingleNode("Address");
                XmlNodeList types = result.SelectNodes("line");
                foreach (XmlNode type in types)
                {
                    Console.WriteLine(type.InnerText);
                }
                XmlNode fmtaddress = result.SelectSingleNode("formatted_address");
            }

Refer to this question for the original source.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.