Parsing XML with pairs of elements using XDocument and LINQ

clifford.duke

I am trying to use XDocument to parse an xml document, however I am pretty new to XML and have only primarily used JSON in the past. So far I can parse each report name but am stuck with trying to parse the parameter list. How do I parse the parameter list when theres no node differentiating the different parameters?

var reports = xml.Descendants("Report").Select(reportElement => new
{
    Name = reportElement.Attribute("Name").Value,
    Parameters = reportElement.Descendants("ParameterList").Select(parameter => new
    {
    })
});

XML:

<ReportList>
    <Report Name="JobNotClose">
        <ParameterList>
            <Name>@StationCode</Name><Value>LAX</Value>
            <Name>@ShipmentType</Name><Value>SE|SI</Value>
        </ParameterList>
    </Report>
    <Report Name="JobWithoutSales">
        <ParameterList>
            <Name>@StationCode</Name><Value>PA</Value>
            <Name>@JobDateFrom</Name><Value>2013-10-1</Value>
            <Name>@JobDateTo</Name><Value>2013-10-31</Value>
        </ParameterList>
    </Report>
</ReportList>
Sergey Berezovskiy

You can use Enumerable.Zip to merge parameter names sequence with parameter values sequence:

var reports = from r in xml.Root.Elements("Report")
              let parameters = r.Element("ParameterList")
              select new {
                  Name = (string)r.Attribute("Name"),
                  Parameters = parameters.Elements("Name")
                                         .Zip(parameters.Elements("Value"), 
                                              (n,v) => new { 
                                                  Name = (string)n, 
                                                  Value = (string)v 
                                              })
              };

That gives following reports collection:

[
  {
    "Name": "JobNotClose",
    "Parameters": [
      {
        "Name": "@StationCode",
        "Value": "LAX"
      },
      {
        "Name": "@ShipmentType",
        "Value": "SE|SI"
      }
    ]
  },
  {
    "Name": "JobWithoutSales",
    "Parameters": [
      {
        "Name": "@StationCode",
        "Value": "PA"
      },
      {
        "Name": "@JobDateFrom",
        "Value": "2013-10-1"
      },
      {
        "Name": "@JobDateTo",
        "Value": "2013-10-31"
      }
    ]
  }
]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Parsing xml using XDocument - Deserialization

From Dev

Linq to Xml: Is XDocument a caching reader?

From Dev

Issue parsing XML string to xDocument

From Dev

Getting attributes from same-named XDocument elements using LINQ

From Dev

How to query a sitemap using an XDocument linq query

From Dev

Parsing XML with pairs of elements using XDocument and LINQ

From Dev

Count of sub-elements using XDocument

From Dev

Adding elements to xml file in C# using XDocument

From Dev

XML compare Elements and Values using LINQ to XML

From Dev

Parsing XML into classes using LINQ in C#

From Dev

XML Parsing Child Elements using xml.etree.ElementTree

From Dev

Read the XML using Linq and check if the elements exist

From Dev

C# LINQ xml parsing using "PreviousNode"

From Dev

parsing xsd from WSDL using LINQ to XML

From Dev

Extracting information from xml using XDOCUMENT and LINQ (C#)

From Dev

Parse and add Elements to XAML using XDocument

From Dev

Parsing nested elements in an XML file using Java

From Dev

Count of sub-elements using XDocument

From Dev

Retrieve a list of specific elements from an XDocument using LINQ

From Dev

how to read nested elements using Linq to xml

From Dev

Parsing XML elements for a TableView

From Dev

XML parsing using LINQ to XML: Multiple descendants with same name

From Dev

XDocument Descendants, using elements value in c#

From Dev

Parsing XML: Finding Interesting Elements Using ElementTree

From Dev

Parsing XML into classes using LINQ in C#

From Dev

parsing xsd from WSDL using LINQ to XML

From Dev

XDocument insert XML data into SQL using Linq and Entity Framework

From Dev

Parsing a XDocument

From Dev

Parsing XML key pairs with Javascript

Related Related

HotTag

Archive