Solved

How to read XML file attributes...

Posted on 2016-10-27
17
96 Views
Last Modified: 2016-10-28
HI

I have a requirement to to read XML below, and i need help with couple of issues i have.

Here is my XML sample:
  <Entities>  
 <Entity Id="899" ExternalId="126" LongName="TINE Yog Melon/pasjon 2,5 kg" EntityTypeId="16" EntityTypeName="Produkt" ContainerId="4" ContainerName="Master katalog" OrganizationName="TINE SA" CategoryId="169" CategoryName="1230" CategoryLongName="TINE FruktYoghurt" CategoryPath="120 » 1230" CategoryLongNamePath="" ParentEntityId="169" ParentExternalId="1230" ParentExtensionEntityId="0" ParentExtensionEntityExternalId="" ParentExtensionEntityContainerId="0" ParentExtensionEntityContainerName="" ParentExtensionEntityCategoryId="0" ParentExtensionEntityCategoryPath="" ParentExtensionEntityCategoryLongNamePath="" Locale="no_NO_B" Action="Add">
      <Attributes>
        <Attribute Id="4041" [u]Name="M3_EAN_Dpak"[/u] LongName="EAN Dpak" InstanceRefId="-1" Sequence="-1" AttributeParentId="4001" AttributeParentName="M3" AttributeType="Simple" AttributeDataType="String" Locale="no_NO_B" Action="Add">
          <Values>
            <Value Id="0" Uom="" ValueRefId="-1" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="no_NO_B" Action="Add"><![CDATA[[u]7038010001260[/u]]]></Value>
          </Values>
        </Attribute>
        <Attribute Id="4051" Name="M3_Artikkelnr" LongName="Artikkelnr" InstanceRefId="-1" Sequence="-1" AttributeParentId="4001" AttributeParentName="M3" AttributeType="Simple" AttributeDataType="String" Locale="no_NO_B" Action="Add">
          <Values>
            <Value Id="0" Uom="" ValueRefId="-1" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="no_NO_B" Action="Add"><![CDATA[126]]></Value>
          </Values>
        </Attribute>
        <Attribute Id="4076" Name="M3_Navn" LongName="M3 Navn" InstanceRefId="-1" Sequence="-1" AttributeParentId="4001" AttributeParentName="M3" AttributeType="Simple" AttributeDataType="String" Locale="no_NO_B" Action="Add">
          <Values>
            <Value Id="0" Uom="" ValueRefId="-1" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="no_NO_B" Action="Add"><![CDATA[2,5 KG YOG. MELON/PASJON SPANN]]></Value>
          </Values>
        </Attribute>
        <Attribute Id="4200" Name="TS_EPD_varenr" LongName="EPD varenr" InstanceRefId="-1" Sequence="-1" AttributeParentId="4003" AttributeParentName="Pricat" AttributeType="Simple" AttributeDataType="String" Locale="no_NO_B" Action="Add">
          <Values>
            <Value Id="0" Uom="" ValueRefId="-1" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="no_NO_B" Action="Add"><![CDATA[2104115]]></Value>
          </Values>
        </Attribute>
      </Attributes>
      <Hierarchies></Hierarchies>
      <Relationships />
      <Extensions></Extensions>
    </Entity>
 </Entities>

Open in new window


I need to read all the attributes in Entety node, and that works fine, but i cant figure out how i can loop thrue all attributes in ChildNodes <Attribute>. I need to read <Attribute Name="xxxxxxx">  and i need to grab the <value> inside CDATA tag.

I need to loop all attributes below, and grab the attrube Name="", and value inside CDATA:
<Attribute Id="4041" [u]Name="M3_EAN_Dpak"[/u] LongName="EAN Dpak" InstanceRefId="-1" Sequence="-1" AttributeParentId="4001" AttributeParentName="M3" AttributeType="Simple" AttributeDataType="String" Locale="no_NO_B" Action="Add">
          <Values>
            <Value Id="0" Uom="" ValueRefId="-1" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="no_NO_B" Action="Add"><![CDATA[[u]7038010001260[/u]]]></Value>
          </Values>
        </Attribute>
        <Attribute Id="4051" Name="M3_Artikkelnr" LongName="Artikkelnr" InstanceRefId="-1" Sequence="-1" AttributeParentId="4001" AttributeParentName="M3" AttributeType="Simple" AttributeDataType="String" Locale="no_NO_B" Action="Add">
          <Values>
            <Value Id="0" Uom="" ValueRefId="-1" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="no_NO_B" Action="Add"><![CDATA[126]]></Value>
          </Values>
        </Attribute>
        <Attribute Id="4076" Name="M3_Navn" LongName="M3 Navn" InstanceRefId="-1" Sequence="-1" AttributeParentId="4001" AttributeParentName="M3" AttributeType="Simple" AttributeDataType="String" Locale="no_NO_B" Action="Add">
          <Values>
            <Value Id="0" Uom="" ValueRefId="-1" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="no_NO_B" Action="Add"><![CDATA[2,5 KG YOG. MELON/PASJON SPANN]]></Value>
          </Values>
        </Attribute>
        <Attribute Id="4200" Name="TS_EPD_varenr" LongName="EPD varenr" InstanceRefId="-1" Sequence="-1" AttributeParentId="4003" AttributeParentName="Pricat" AttributeType="Simple" AttributeDataType="String" Locale="no_NO_B" Action="Add">
          <Values>
            <Value Id="0" Uom="" ValueRefId="-1" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="no_NO_B" Action="Add"><![CDATA[2104115]]></Value>
          </Values>
        </Attribute>

Open in new window



Any help to solve this will be appriciated, and thanks in advance...
0
Comment
Question by:Adnan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 4
  • 3
17 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41862947
What classes are you using to read the Attributes anode and Child nodes. Are you using Linq to XML?
0
 

Author Comment

by:Adnan
ID: 41862954
Iam using XMLDocument, se code below:
 private static void readXMLFile(SPODataContext sp, string filePath)
        {
            //Create the XmlDocument.
            XmlDocument doc = new XmlDocument();
            doc.Load(filePath);

            XmlNodeList elemList = doc.GetElementsByTagName("Entity");
            for (int i = 0; i < elemList.Count; i++)
            {
                XmlNode elm = elemList[i];
                getEntity(elm);
            }
        }

        private static void getEntity(XmlNode elm)
        {
            for (int i = 0; i < elm.Attributes.Count; i++)
            {
                XmlAttribute entetyAttri = elm.Attributes[i];
                switch (entetyAttri.Name)
                {
                    case "Id":
                        Console.WriteLine(entetyAttri.Name);
                        Console.WriteLine(entetyAttri.Value);
                        break;
                    case "ExternalId":
                        Console.WriteLine(entetyAttri.Name);
                        Console.WriteLine(entetyAttri.Value);
                        break;
                    case "LongName":
                        Console.WriteLine(entetyAttri.Name);
                        Console.WriteLine(entetyAttri.Value);
                        break;
                    case "EntityTypeId":
                        Console.WriteLine(entetyAttri.Name);
                        Console.WriteLine(entetyAttri.Value);
                        break;
                    case "EntityTypeName":
                        Console.WriteLine(entetyAttri.Name);
                        Console.WriteLine(entetyAttri.Value);
                        break;
                    case "ContainerId":
                        Console.WriteLine(entetyAttri.Name);
                        Console.WriteLine(entetyAttri.Value);
                        break;
                    case "ContainerName":
                        Console.WriteLine(entetyAttri.Name);
                        Console.WriteLine(entetyAttri.Value);
                        break;
                    case "OrganizationName":
                        Console.WriteLine(entetyAttri.Name);
                        Console.WriteLine(entetyAttri.Value);
                        break;
                    case "CategoryId":
                        Console.WriteLine(entetyAttri.Name);
                        Console.WriteLine(entetyAttri.Value);
                        break;
                    case "CategoryName":
                        Console.WriteLine(entetyAttri.Name);
                        Console.WriteLine(entetyAttri.Value);
                        break;
                    case "CategoryLongName":
                        Console.WriteLine(entetyAttri.Name);
                        Console.WriteLine(entetyAttri.Value);
                        break;
                    case "CategoryPath":
                        Console.WriteLine(entetyAttri.Name);
                        Console.WriteLine(entetyAttri.Value);
                        break;
                    case "CategoryLongNamePath":
                        Console.WriteLine(entetyAttri.Name);
                        Console.WriteLine(entetyAttri.Value);
                        break;
                    case "ParentEntityId":
                        Console.WriteLine(entetyAttri.Name);
                        Console.WriteLine(entetyAttri.Value);
                        break;
                    case "ParentExternalId":
                        Console.WriteLine(entetyAttri.Name);
                        Console.WriteLine(entetyAttri.Value);
                        break;
                    case "ParentExtensionEntityId":
                        Console.WriteLine(entetyAttri.Name);
                        Console.WriteLine(entetyAttri.Value);
                        break;
                    case "ParentExtensionEntityExternalId":
                        Console.WriteLine(entetyAttri.Name);
                        Console.WriteLine(entetyAttri.Value);
                        break;
                    case "ParentExtensionEntityContainerId":
                        Console.WriteLine(entetyAttri.Name);
                        Console.WriteLine(entetyAttri.Value);
                        break;
                    case "ParentExtensionEntityContainerName":
                        Console.WriteLine(entetyAttri.Name);
                        Console.WriteLine(entetyAttri.Value);
                        break;
                    case "ParentExtensionEntityCategoryId":
                        Console.WriteLine(entetyAttri.Name);
                        Console.WriteLine(entetyAttri.Value);
                        break;
                    case "ParentExtensionEntityCategoryPath":
                        Console.WriteLine(entetyAttri.Name);
                        Console.WriteLine(entetyAttri.Value);
                        break;
                    case "ParentExtensionEntityCategoryLongNamePath":
                        Console.WriteLine(entetyAttri.Name);
                        Console.WriteLine(entetyAttri.Value);
                        break;
                    case "Locale":
                        Console.WriteLine(entetyAttri.Name);
                        Console.WriteLine(entetyAttri.Value);
                        break;
                    default:
                        Console.WriteLine("Default case");
                        break;
                }
            }
            XmlNodeList celm = elm.ChildNodes;
            getAttributes(celm);
        }


        public static int count;
        private static void getAttributes(XmlNodeList celm)
        {
            count = celm.Count;
            for (int i = 0; i <= celm.Count - 1; i++)
            {
                var childNd = celm[i].Name;
                if (childNd == "Attributes")
                {
                    for (int ci = 0; ci < celm.Count; ci++)
                    {
                        XmlNode clm = celm[ci].ChildNodes[ci];
                        if(clm != null)
                        {
                            for (int att = 0; att < clm.Attributes.Count; att++)
                            {
                                if (clm.Attributes[att].NodeType.ToString() == "Attribute")
                                {
                                    XmlAttribute entetyAttri = clm.Attributes[att];
                                    switch (entetyAttri.Name)
                                    {
                                        case "Name":
                                            Console.WriteLine(entetyAttri.Name);
                                            Console.WriteLine(entetyAttri.Value);
                                            break;

                                        default:
                                            Console.WriteLine("Default case");
                                            break;
                                    }
                                }
                            }
                        }
                    }
                }

                count++;
            }
        }

Open in new window

0
 

Author Comment

by:Adnan
ID: 41862964
Here is the XML full sample, with only one Entity, but there gona be hundreds of them, and attrube can be 1 or more...:

<?xml version="1.0" encoding="utf-8"?>
<Data Schema="RSXML4.1" xmlns="http://schemas.riversand.com/mdmcenter" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schemas.riversand.com/mdmcenter/RSXml4.1.xsd">
  <Entities>
    <Entity Id="899" ExternalId="126" LongName="TINE Yog Melon/pasjon 2,5 kg" EntityTypeId="16" EntityTypeName="Produkt" ContainerId="4" ContainerName="Master katalog" OrganizationName="TINE SA" CategoryId="169" CategoryName="1230" CategoryLongName="TINE FruktYoghurt" CategoryPath="120 » 1230" CategoryLongNamePath="" ParentEntityId="169" ParentExternalId="1230" ParentExtensionEntityId="0" ParentExtensionEntityExternalId="" ParentExtensionEntityContainerId="0" ParentExtensionEntityContainerName="" ParentExtensionEntityCategoryId="0" ParentExtensionEntityCategoryPath="" ParentExtensionEntityCategoryLongNamePath="" Locale="no_NO_B" Action="Add">
      <Attributes>
        <Attribute Id="4041" Name="M3_EAN_Dpak" LongName="EAN Dpak" InstanceRefId="-1" Sequence="-1" AttributeParentId="4001" AttributeParentName="M3" AttributeType="Simple" AttributeDataType="String" Locale="no_NO_B" Action="Add">
          <Values>
            <Value Id="0" Uom="" ValueRefId="-1" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="no_NO_B" Action="Add"><![CDATA[7038010001260]]></Value>
          </Values>
        </Attribute>
        <Attribute Id="4051" Name="M3_Artikkelnr" LongName="Artikkelnr" InstanceRefId="-1" Sequence="-1" AttributeParentId="4001" AttributeParentName="M3" AttributeType="Simple" AttributeDataType="String" Locale="no_NO_B" Action="Add">
          <Values>
            <Value Id="0" Uom="" ValueRefId="-1" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="no_NO_B" Action="Add"><![CDATA[126]]></Value>
          </Values>
        </Attribute>
        <Attribute Id="4076" Name="M3_Navn" LongName="M3 Navn" InstanceRefId="-1" Sequence="-1" AttributeParentId="4001" AttributeParentName="M3" AttributeType="Simple" AttributeDataType="String" Locale="no_NO_B" Action="Add">
          <Values>
            <Value Id="0" Uom="" ValueRefId="-1" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="no_NO_B" Action="Add"><![CDATA[2,5 KG YOG. MELON/PASJON SPANN]]></Value>
          </Values>
        </Attribute>
        <Attribute Id="4200" Name="TS_EPD_varenr" LongName="EPD varenr" InstanceRefId="-1" Sequence="-1" AttributeParentId="4003" AttributeParentName="Pricat" AttributeType="Simple" AttributeDataType="String" Locale="no_NO_B" Action="Add">
          <Values>
            <Value Id="0" Uom="" ValueRefId="-1" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="no_NO_B" Action="Add"><![CDATA[2104115]]></Value>
          </Values>
        </Attribute>
      </Attributes>
      <Hierarchies></Hierarchies>
      <Relationships />
      <Extensions></Extensions>
    </Entity>
</Entities>
</Data>

Open in new window

0
Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41862969
Can you use XDocument object instead of XMLDocument objects?
0
 

Author Comment

by:Adnan
ID: 41862971
Yes why not, but i havent used XDocument before, any sample you can show  for this XML?
0
 

Author Comment

by:Adnan
ID: 41862991
I tried it this way, but its returning null:

 private static void readXMLFile(SPODataContext sp, string filePath)
        {
           try
            {
                XDocument xdoc = XDocument.Load(filePath);
                var groupElements = from el in xdoc.Descendants().Elements("Entity") select el;

               // Console.WriteLine("");
            }
            catch(Exception exp)
            {
                Console.WriteLine(exp);
            }
        }

Open in new window

0
 

Author Comment

by:Adnan
ID: 41863011
Why iam getting result "null"  when i use XDocuemnt class:

 XDocument xdoc = XDocument.Load(filePath);
                //var groupElements = from el in xdoc.Descendants().Elements("Entity") select el;
                var query = xdoc.Descendants("Entities");

Open in new window



"Enumeration yielded no results"
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41863015
It is most likely due to the xml namespace in the root node.

What is the Encoding of the full XML document you posted? There seems to be an invalid character on line 4 character position 288, a double greater then sign as one character hex value BB.
0
 

Author Comment

by:Adnan
ID: 41863025
Sorry i cant see the namespace, it is Encoding UTF-8

XML:

<?xml version="1.0" encoding="utf-8"?>
<Data Schema="RSXML4.1" xmlns="http://schemas.riversand.com/mdmcenter" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schemas.riversand.com/mdmcenter/RSXml4.1.xsd">
  <Entities>
    <Entity Id="899" ExternalId="126" LongName="TINE Yog Melon/pasjon 2,5 kg" EntityTypeId="16" EntityTypeName="Produkt" ContainerId="4" ContainerName="Master katalog" OrganizationName="TINE SA" CategoryId="169" CategoryName="1230" CategoryLongName="TINE FruktYoghurt" CategoryPath="120 » 1230" CategoryLongNamePath="" ParentEntityId="169" ParentExternalId="1230" ParentExtensionEntityId="0" ParentExtensionEntityExternalId="" ParentExtensionEntityContainerId="0" ParentExtensionEntityContainerName="" ParentExtensionEntityCategoryId="0" ParentExtensionEntityCategoryPath="" ParentExtensionEntityCategoryLongNamePath="" Locale="no_NO_B" Action="Add">
      <Attributes>
        <Attribute Id="4041" Name="M3_EAN_Dpak" LongName="EAN Dpak" InstanceRefId="-1" Sequence="-1" AttributeParentId="4001" AttributeParentName="M3" AttributeType="Simple" AttributeDataType="String" Locale="no_NO_B" Action="Add">
          <Values>
            <Value Id="0" Uom="" ValueRefId="-1" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="no_NO_B" Action="Add"><![CDATA[7038010001260]]></Value>
          </Values>
        </Attribute>
        <Attribute Id="4051" Name="M3_Artikkelnr" LongName="Artikkelnr" InstanceRefId="-1" Sequence="-1" AttributeParentId="4001" AttributeParentName="M3" AttributeType="Simple" AttributeDataType="String" Locale="no_NO_B" Action="Add">
          <Values>
            <Value Id="0" Uom="" ValueRefId="-1" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="no_NO_B" Action="Add"><![CDATA[126]]></Value>
          </Values>
        </Attribute>
        <Attribute Id="4076" Name="M3_Navn" LongName="M3 Navn" InstanceRefId="-1" Sequence="-1" AttributeParentId="4001" AttributeParentName="M3" AttributeType="Simple" AttributeDataType="String" Locale="no_NO_B" Action="Add">
          <Values>
            <Value Id="0" Uom="" ValueRefId="-1" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="no_NO_B" Action="Add"><![CDATA[2,5 KG YOG. MELON/PASJON SPANN]]></Value>
          </Values>
        </Attribute>
        <Attribute Id="4200" Name="TS_EPD_varenr" LongName="EPD varenr" InstanceRefId="-1" Sequence="-1" AttributeParentId="4003" AttributeParentName="Pricat" AttributeType="Simple" AttributeDataType="String" Locale="no_NO_B" Action="Add">
          <Values>
            <Value Id="0" Uom="" ValueRefId="-1" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="no_NO_B" Action="Add"><![CDATA[2104115]]></Value>
          </Values>
        </Attribute>
      </Attributes>
      <Hierarchies></Hierarchies>
      <Relationships />
      <Extensions></Extensions>
    </Entity>
</Entities>
</Data>

Open in new window

0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 41863054
Fernando Soto is talking about
xmlns="http://schemas.riversand.com/mdmcenter" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

Open in new window

in the Data root.

And I'm pretty sure he's right. If you remove namespaces from your xml you'll be able to read elements.

Or you will need to use XNamespace Class.
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 41863065
Try something like:
    class Program
    {
        static void Main(string[] args)
        {
            readXMLFile("data.xml");
            Console.ReadLine();
        }

        private static void readXMLFile(string filePath)
        {
            try
            {
                XDocument xdoc = XDocument.Load(filePath);
                XNamespace nsSys = "http://schemas.riversand.com/mdmcenter";
                var entities = from el in xdoc.Descendants(nsSys + "Entity") 
                                    select new
                                    {
                                        Id=el.Attribute("Id").Value,
                                        LongName=el.Attribute("LongName").Value
                                    };

                foreach (var p in entities)
                    Console.WriteLine(p.ToString());

                Console.WriteLine("");
            }
            catch (Exception exp)
            {
                Console.WriteLine(exp);
            }
        }
    }

Open in new window

0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41863066
When you did this
 XDocument xdoc = XDocument.Load(filePath);
                //var groupElements = from el in xdoc.Descendants().Elements("Entity") select el;
                var query = xdoc.Descendants("Entities");

Open in new window

did you not get an error? If not what XML document did you use?
0
 

Author Comment

by:Adnan
ID: 41863085
I managed to read the node <Entity>.

This code sample worked:
 private static void readXMLFile(SPODataContext sp, string filePath)
        {
           try
            {
                XDocument xdoc = XDocument.Load(filePath);
                var query = from d in xdoc.Descendants()
                            where d.Name.LocalName == "Entity"
                            select d;


                Console.WriteLine(query);
            }
            catch (Exception exp)
            {
                Console.WriteLine(exp);
            }
        }

Open in new window

0
 

Author Comment

by:Adnan
ID: 41863093
Perfect anarki_jimbel, your sample worked as charm, but how can i grab the value from choldnodes:

f.exp

<Attribute Id="4041" Name="M3_EAN_Dpak" LongName="EAN Dpak" InstanceRefId="-1" Sequence="-1" AttributeParentId="4001" AttributeParentName="M3" AttributeType="Simple" AttributeDataType="String" Locale="no_NO_B" Action="Add">
          <Values>
            <Value Id="0" Uom="" ValueRefId="-1" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="no_NO_B" Action="Add"><![CDATA[7038010001260]]></Value>
          </Values>
        </Attribute>

Open in new window



I need to grab the <Attribute> Name="M3_EAN_Dpak"
And value [CDATA[7038010001260]]

it can be one or more Attributes in one Entetity.
0
 

Author Comment

by:Adnan
ID: 41863097
to be more correct i need to grab Name and CDATA for each Attribute...
0
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 41863151
Honestly, I'm not that experienced in this stuff. So my code might be not good enough, may be there are some more clever ways.
Anyway, try this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.IO;
using System.Xml;

namespace XDocTest
{
    class Program
    {
        static void Main(string[] args)
        {
            readXMLFile("data.xml");
            Console.ReadLine();
        }

        public class Entity
        {
            public string Id { get; set; }
            public string LongName { get; set; }
            public IEnumerable<Attribute> Attributes { get; set; }

        }
        public class Attribute
        {
            public string Id { get; set; }
            public string Name { get; set; }
            public IEnumerable<AttributeValue> Values { get; set; }
        }

        public class AttributeValue
        {
            public string CData { get; set; }
        }
        private static void readXMLFile(string filePath)
        {
            try
            {
                XDocument xdoc = XDocument.Load(filePath);
                XNamespace nsSys = "http://schemas.riversand.com/mdmcenter";
                var entities = from el in xdoc.Descendants(nsSys + "Entity") 
                                    select new Entity
                                    {
                                        Id=el.Attribute("Id").Value,
                                        LongName=el.Attribute("LongName").Value,
                                        Attributes = from a in el.Descendants(nsSys+"Attribute")
                                        select new Attribute
                                        {
                                            Id = a.Attribute("Id").Value,
                                            Name = a.Attribute("Name").Value,
                                            Values = from v in a.Descendants(nsSys + "Value")
                                                     select new AttributeValue
                                                     {
                                                         CData = v.Value
                                                     }
                                        }
                                    };

                foreach (Entity p in entities)
                {
                    Console.WriteLine(p.Id + " / " + p.LongName);
                    foreach (Attribute a in p.Attributes)
                    {
                        Console.WriteLine("\t" + "Name = " + a.Name);
                        foreach (AttributeValue v in a.Values)
                        {
                            Console.WriteLine("\t\t" + "CDATA = " + v.CData);
                        }
                    }
                }
                    

                Console.WriteLine("");
            }
            catch (Exception exp)
            {
                Console.WriteLine(exp);
            }
        }
    }
}

Open in new window


Output
0
 

Author Closing Comment

by:Adnan
ID: 41863487
Hi,

This did the job for me, now i understand bit more how to use XDocument class, thanks alot for your help....
0

Featured Post

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question