Looping through XML element c#

Shabbir Daruwala 176 Reputation points
2021-10-28T15:34:20.59+00:00

How to access EntityAttributeValue -> Value and EntityAttributeValue -> Attribute - > Name using c#

 <AttributeValues>
    <EntityAttributeValue>
      <Value />
      <Attribute>
        <Id>1</Id>
        <Name>Internet Filter 1</Name>
        <AttributeType>other</AttributeType>
      </Attribute>
      <AttributeValueId>0</AttributeValueId>
      <ProductTypeId xsi:nil="true" />
    </EntityAttributeValue>
    <EntityAttributeValue>
      <Value />
      <Attribute>
        <Id>0</Id>
        <Name>Product Type</Name>
        <AttributeType>category</AttributeType>
      </Attribute>
      <AttributeValueId>0</AttributeValueId>
      <ProductTypeId xsi:nil="true" />
    </EntityAttributeValue>
    <EntityAttributeValue>
      <Value>Beauty</Value>
      <Attribute>
        <Id>1</Id>
        <Name>Department</Name>
        <AttributeType>global</AttributeType>
      </Attribute>
      <AttributeValueId>7</AttributeValueId>
      <ProductTypeId xsi:nil="true" />
    </EntityAttributeValue>
    <EntityAttributeValue>
      <Value>Women</Value>
      <Attribute>
        <Id>2</Id>
        <Name>Gender</Name>
        <AttributeType>global</AttributeType>
      </Attribute>
      <AttributeValueId>1</AttributeValueId>
      <ProductTypeId xsi:nil="true" />
    </EntityAttributeValue>
    <EntityAttributeValue>
      <Value>CHANEL</Value>
      <Attribute>
        <Id>8</Id>
        <Name>Brand</Name>
        <AttributeType>global</AttributeType>
      </Attribute>
      <AttributeValueId>2958</AttributeValueId>
      <ProductTypeId xsi:nil="true" />
    </EntityAttributeValue>
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,237 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 24,286 Reputation points Microsoft Vendor
    2021-10-29T02:12:30.123+00:00

    @Shabbir Daruwala , Welcome to Microsoft Q&A,

    You can save your xml text to a xml file. Based on my test, the xml you provided is need to modify, the following xml is an example.

    <?xml version="1.0" encoding="UTF-8"?>  
    <AttributeValues xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  
         <EntityAttributeValue>  
           <Value />  
           <Attribute>  
             <Id>1</Id>  
             <Name>Internet Filter 1</Name>  
             <AttributeType>other</AttributeType>  
           </Attribute>  
           <AttributeValueId>0</AttributeValueId>  
           <ProductTypeId xsi:nil="true" />  
         </EntityAttributeValue>  
         <EntityAttributeValue>  
           <Value />  
           <Attribute>  
             <Id>0</Id>  
             <Name>Product Type</Name>  
             <AttributeType>category</AttributeType>  
           </Attribute>  
           <AttributeValueId>0</AttributeValueId>  
           <ProductTypeId xsi:nil="true" />  
         </EntityAttributeValue>  
         <EntityAttributeValue>  
           <Value>Beauty</Value>  
           <Attribute>  
             <Id>1</Id>  
             <Name>Department</Name>  
             <AttributeType>global</AttributeType>  
           </Attribute>  
           <AttributeValueId>7</AttributeValueId>  
           <ProductTypeId xsi:nil="true" />  
         </EntityAttributeValue>  
         <EntityAttributeValue>  
           <Value>Women</Value>  
           <Attribute>  
             <Id>2</Id>  
             <Name>Gender</Name>  
             <AttributeType>global</AttributeType>  
           </Attribute>  
           <AttributeValueId>1</AttributeValueId>  
           <ProductTypeId xsi:nil = "true" />  
         </EntityAttributeValue>  
         <EntityAttributeValue>  
           <Value>CHANEL</Value>  
           <Attribute>  
             <Id>8</Id>  
             <Name>Brand</Name>  
             <AttributeType>global</AttributeType>  
           </Attribute>  
           <AttributeValueId>2958</AttributeValueId>  
           <ProductTypeId xsi:nil="true" />  
         </EntityAttributeValue>  
       </AttributeValues>  
    

    Then, you could refer to the following code to use linq to xml to get the value you wanted.

    private void button1_Click(object sender, EventArgs e)  
            {  
                XDocument doc = XDocument.Load("D:\\test.xml");  
                var values = doc.Descendants("EntityAttributeValue").Select(i=>i.Element("Attribute").Element("Name").Value);  
                foreach (var item in values)  
                {  
                    listBox1.Items.Add(item.ToString());  
                }  
                 
            }  
    

    Result:

    144765-image.png


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. P a u l 10,406 Reputation points
    2021-10-28T21:02:29.567+00:00

    You can just load your XML into an XmlDocument and then traverse the tree for the elements you want:

    using System.Xml;
    
    var xml = @"
       <AttributeValues xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">
         <EntityAttributeValue>
           <Value />
           <Attribute>
             <Id>1</Id>
             <Name>Internet Filter 1</Name>
             <AttributeType>other</AttributeType>
           </Attribute>
           <AttributeValueId>0</AttributeValueId>
           <ProductTypeId xsi:nil=""true"" />
         </EntityAttributeValue>
         <EntityAttributeValue>
           <Value />
           <Attribute>
             <Id>0</Id>
             <Name>Product Type</Name>
             <AttributeType>category</AttributeType>
           </Attribute>
           <AttributeValueId>0</AttributeValueId>
           <ProductTypeId xsi:nil=""true"" />
         </EntityAttributeValue>
         <EntityAttributeValue>
           <Value>Beauty</Value>
           <Attribute>
             <Id>1</Id>
             <Name>Department</Name>
             <AttributeType>global</AttributeType>
           </Attribute>
           <AttributeValueId>7</AttributeValueId>
           <ProductTypeId xsi:nil=""true"" />
         </EntityAttributeValue>
         <EntityAttributeValue>
           <Value>Women</Value>
           <Attribute>
             <Id>2</Id>
             <Name>Gender</Name>
             <AttributeType>global</AttributeType>
           </Attribute>
           <AttributeValueId>1</AttributeValueId>
           <ProductTypeId xsi:nil = ""true"" />
         </EntityAttributeValue>
         <EntityAttributeValue>
           <Value>CHANEL</Value>
           <Attribute>
             <Id>8</Id>
             <Name>Brand</Name>
             <AttributeType>global</AttributeType>
           </Attribute>
           <AttributeValueId>2958</AttributeValueId>
           <ProductTypeId xsi:nil=""true"" />
         </EntityAttributeValue>
       </AttributeValues>
    ";
    
    StringReader reader = new StringReader(xml);
    XmlDocument document = new XmlDocument();
    
    document.Load(reader);
    
    XmlNodeList entities = document.SelectNodes("/AttributeValues/EntityAttributeValue");
    
    foreach (XmlNode entity in entities) {
        string name = entity["Attribute"]["Name"].InnerText;
        string value = entity["Value"].InnerText;
    
        Console.WriteLine($"Name: {name}, Value: {value}");
    }
    

    By the way I had to add an xmlns to the AttributeValues root element. If your snippet is nested inside a larger document you may need need that, it's just so xsi:nil is defined for your ProductTypeId to use.

    1 person found this answer helpful.
    0 comments No comments