sectorchan
sectorchan

Reputation: 9

Query all XML Elements and subElements with the corresponding Value and Name

I have this XML File with the following content:

<?xml version="1.0" encoding="utf-8" ?>
<file>
  <root>
    <Sample-CA1-John>
      <Name>John</Name>
      <surname>Doe</surname>
      <type>CA</type>
      <age>30</age>
      <phone>
        <mobile>001555123456</mobile>
        <landline>001666147258</landline>
      </phone>
      <address>
      <private>SF-Railroad2233</private>
      <work>SF-PaloAlto</work>
          </address>
    </Sample-CA1-John>
    <Sample-CA2>
      <Name>Jane</Name>
      <surname>Doe</surname>
      <type>INT</type>
      <age>20</age>
      <phone>
        <mobile>001555456789</mobile>
        <landline>001666258369</landline>
      </phone>
      <address>
      <private>NY-AppleAve5</private>
      <work>NY-Brooklyn</work>
    </address>
    </Sample-CA2>
  </root>
</file>

From the suggested autoclose post, the accepted post from XElement to get all sub-element node names and values should solve my question Partially it does (beside the fact that my approach result in the same) With the code:

static void Main(){
    string xml3 = "xml3.xml";
    var list = from x in XElement.Load(xml3).Elements("root")
            select new
            {
                Name = x.Name,
                Value = (string)x
            };       
    foreach (var item in list) 
    { 
        Console.WriteLine("Item Name: {0} - Item Value: {1}", item.Name, item.Value); 
    }
}

The result is:

Item Name: root - Item Value: JohnDoeCA30001555123456001666147258SF-Railroad2233SF-PaloAltoJaneDoeINT20001555456789001666258369NY-AppleAve5NY-Brooklyn

While this code

static void Main(){
    string xml3 = "xml3.xml";
    string selection = "Sample-CA1-John";
    var doc4 = XDocument.Load(xml3);
    var csFiles3 = doc4.Descendants(selection)
        .Where(n => n.Element("type")?.Value == "CA")
        .ToList();
    foreach (var cs in csFiles3)
    {
        Console.WriteLine(cs);
    }
}

Returns:

<Sample-CA1-John>
  <Name>John</Name>
  <surname>Doe</surname>
  <type>CA</type>
  <age>30</age>
  <phone>
    <mobile>001555123456+</mobile>
    <landline>001666147258</landline>
  </phone>
  <address>
    <private>SF-Railroad2233</private>
    <work>SF-PaloAlto</work>
  </address>
</Sample-CA1-John>

With this code:

XDocument doc = XDocument.Load(xml3);
IEnumerable<XElement> filteredElements = doc.Descendants()
        .Where(n => n.Element("type")?.Value == "CA");
foreach (XElement elements in filteredElements)
{
    Console.WriteLine("Name: {0} - Value: {1}", elements.Name,     elements.Value);
}

I get a single item with all values in the .Value but only the .Where "tag" as Element.Name

Name: Sample-CA1-John - Value: JohnDoeCA30001555123456+001666147258SF-Railroad2233SF-PaloAlto

With no approach I've tried it differs from the results above.

What I want is to get access of each element.Name and element.Value. Depending on a search tag. Why? Because I want to read the XML and write each value into an own field.

Upvotes: 0

Views: 44

Answers (0)

Related Questions