Solved

How to read XML file attributes...

Posted on 2016-10-27
17
52 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
  • 10
  • 4
  • 3
17 Comments
 
LVL 62

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
 
LVL 62

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 62

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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 29

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 29

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 62

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 29

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now