-2
<Products>
 <Product>
      <Product_code>
           <![CDATA[ 9.077 ]]>
      </Product_code>
      <Price2>799.99</Price2>
                <variants>
                          <variant>
                                    <spec name="Renk">White</spec>
                                    <productCode>
                                              <![CDATA[ 9.0771933 ]]>
                                    </productCode>
                                    <picture>
                                              <![CDATA[ image/data/resimler/hakiki-deri-cz-saracli-topuklu-kadin-cizme-8316.jpg ]]>
                                    </picture>
                                    <picture>
                                              <![CDATA[ image/data/resimler/hakiki-deri-cz-saracli-topuklu-kadin-cizme-8314.jpg ]]>
                                    </picture>
                          </variant>
                          <variant>
                                    <spec name="Renk">Black</spec>
                                    <productCode>
                                              <![CDATA[ 9.0771734 ]]>
                                    </productCode>
                                    <picture>
                                              <![CDATA[ image/data/resimler/hakiki-deri-cz-saracli-topuklu-kadin-cizme-8316.jpg ]]>
                                    </picture>
                                    <picture>
                                              <![CDATA[ image/data/resimler/hakiki-deri-cz-saracli-topuklu-kadin-cizme-8314.jpg ]]>
                                    </picture>
                          </variant>
                </variants>
      </Product>
</Products>

This is my XML sample:
It's about products with Product Code, Color, Variant Code, and Picture

I want to get first the Product_codes then all Variants of that Product_code

For example:


Product_code: 9.077

Price2: 799.99


Renk: White

productCode: 9.0771933

picture1: link1

picture2: link2


Renk: Black

productCode: 9.0771734

picture1: link1

picture2: link2

XmlDocument xmlDoc = new XmlDocument();
        XmlDocument xDoc = new XmlDocument();
        xmlDoc.Load("eticaret.xml");
        XmlNodeList nodeList = xmlDoc.DocumentElement.SelectNodes("/Products/Product");

        foreach (XmlNode tu in xmlDoc.SelectNodes("/Products/Product"))
        {
            MessageBox.Show("tu = " + tu.SelectSingleNode("Product_code").InnerText);
            foreach (XmlNode tuv in tu.SelectNodes("/Products/Product/variants/variant"))
            {

                MessageBox.Show("tuv = " + tuv.SelectSingleNode("productCode").InnerText + "   -   " + tuv.SelectSingleNode("spec[@name='Renk']").InnerText;
            }
        }

I used this code

It actually works but: The first gives the information of the first part The following shows only the product variants It no longer shows the first information

  • 1
    Welcome to Stack Overflow! Can you show us what you already tried and where specifically you got stuck? – Klaus Gütter Aug 29 '20 at 06:50
  • I used "foreach XmlNode" for get ( Product_code: 9.077 and Price2: 799.99) then i think i should use second foreach to get (Renk, productCode, picture1, picture2) of (Product_code: 9.077) – Farboud Farhoudi Aug 29 '20 at 06:59
  • Deserializing Xml is one of the option . [This stack overflow link will help you](https://stackoverflow.com/questions/364253/how-to-deserialize-xml-document) – Biju Kalanjoor Aug 29 '20 at 07:00
  • 1
    Please add your attempt as formatted code to your question. – Klaus Gütter Aug 29 '20 at 07:01
  • Biju Kalanjoor, I do this, It's ok But problem is in second childnode (Renk, productCode, picture1, picture2) total product : 1233 Each product has 5 or 5 variants – Farboud Farhoudi Aug 29 '20 at 07:05

2 Answers2

1

There is a problem with this line:

foreach (XmlNode tuv in tu.SelectNodes("/Products/Product/variants/variant"))

It selects all variants from all products, not only those below the current tu node. Change it to :

foreach (XmlNode tuv in tu.SelectNodes("variants/variant"))

This will select the nodes relative to the current tu node.

To select the pictures, you can use the following code:

foreach (XmlNode picture in tuv.SelectNodes("picture"))
{
    Console.WriteLine("  " + picture.InnerText);
}
Klaus Gütter
  • 11,151
  • 6
  • 31
  • 36
0

Using xml linq :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            List<Product> products = doc.Descendants("Product")
                .Select(x => new Product()
                {
                    code = (string)x.Element("Product_code"),
                    price = (decimal)x.Element("Price2"),
                    variants = x.Descendants("variant")
                       .Select(y => new Variant()
                       {
                           renk = (string)y.Element("spec"),
                           code = (string)y.Element("productCode"),
                           urls = y.Elements("picture").Select(z => (string)z).ToList()
                       }).ToList()
                }).ToList();

        }
    }
    public class Product
    {
        public string code { get; set; }
        public decimal price { get; set;}
        public List<Variant> variants { get; set;}

    }
    public class Variant
    {
        public string renk { get; set; }
        public string code { get; set; }
        public List<string> urls { get; set; }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20