简体   繁体   中英

Read multiple child nodes of xml via c#

<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_code s then all Variant s 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

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);
}

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; }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM