Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing XElement

I am newbie to LINQ/XML. I just learned that Language integrated queries can be used for parsing xml. I am trying to parse the following XML structure and I need help

==================================

<config>

<params>
  <tp name="abc">yes</tp>
  <tp name="aee">no</tp>
  <tp name="bbc">no</tp>
  <tp name="ccb">yes</tp>
</params>

<nits>
  <tn name="kjh">
    <min>44</min>
    <max>98</max> 
  </tn>

  <tn name="klm">
    <min>55</min>
    <max>88</max> 
  </tn>

  <tn name="hhh">
    <min>44</min>
    <max>98</max> 
  </tn>
</nits>

<params>
  <tp name="lml">no</tp>
  <tp name="rre">yes</tp>
  <tp name="rst">no</tp>
  <tp name="wee">yes</tp>
</params>

<nits>
  <tn name="adf">
    <min>44</min>
    <max>98</max> 
  </tn>

  <tn name="ddd">
    <min>42</min>
    <max>92</max> 
  </tn>

  <tn name="kjj">
    <min>92</min>
    <max>98</max> 
  </tn>
</nits>

</config>

==================================

Output needed :

My objective is to show the above data as key value pairs by the category in a text file or a data grid etc.

<Params>
abc : yes
aee : no
bbc : yes
...
...
...
...


<nits>

kjh: 44 , 98
klm: 55 , 88
...
...
kjj: 92 , 98
ddd: 42 , 92
...

The code I have written so far is,

static void QueryTheData(XDocument doc)
        {
            try
            {
                var a = doc.Descendants("config").Elements("params");
                var b = doc.Descendants("config").Elements("nits");

                var c = doc.Elements("tp");
                var d = doc.Elements("tn");

               /* to do :  parse the elements into key value pairs */
               /* Need hint or help to proceed to get key value pairs from xml data

            }
             catch(Exception e)
            {
                 ....
            }               
 }

ANy good pointers for beginner's LINQ/XML C# is also needed.

thanks in advance, ak

like image 573
a k Avatar asked Dec 31 '25 09:12

a k


1 Answers

I came up with this:

using System.Linq;
using System.Xml.Linq;
using System.Xml.XPath;
using System.IO;
using System;

public class Program
{
    public static void Main(string[] args)
    {
        using (var fs = new StreamReader("./test.xml"))
        {
            var doc = XDocument.Load(fs);

            var parms = doc.Root.XPathSelectElements("params/tp")
                .ToDictionary(el => el.Attribute("name").Value, el => el.Value);

            var nits = doc.Root.XPathSelectElements("nits/tn")
                .Select(el => new {
                        Name = el.Attribute("name").Value,
                        Min  = (int) el.Element("min"),
                        Max  = (int) el.Element("max")
                    }).ToList();

            foreach (var kvp in parms)
                Console.WriteLine("{0}: {1}", kvp.Key, kvp.Value);

            foreach (var nit in nits
                    .OrderBy(nit => nit.Name)
                    .ThenBy(nit => nit.Max))
            {
                Console.WriteLine("{0}: {1} {2}", nit.Name, nit.Min, nit.Max);
            }
        }
    }
}

This shows you a few of the ingredients, including different approaches to storing params/nits in collection types. Output:

abc: yes
aee: no
bbc: no
ccb: yes
lml: no
rre: yes
rst: no
wee: yes
adf: 44 98
ddd: 42 92
hhh: 44 98
kjh: 44 98
kjj: 92 98
klm: 55 88
like image 133
sehe Avatar answered Jan 01 '26 21:01

sehe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!