question

ShabbirDaruwala-7855 avatar image
0 Votes"
ShabbirDaruwala-7855 asked JackJJun-MSFT answered

Looping through XML element c#

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>
dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JackJJun-MSFT avatar image
0 Votes"
JackJJun-MSFT answered

@ShabbirDaruwala-7855, 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.


image.png (5.3 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Paul-5034 avatar image
0 Votes"
Paul-5034 answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.