簡體   English   中英

通過c#讀取xml的多個子節點

[英]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>

這是我的 XML 示例:
它是關於具有Product CodeColorVariant CodePicture的產品

我想首先獲得Product_code然后是該Product_code的所有Variant

例如:


產品代碼 9.077

價格2: 799.99


倫克:白色

產品代碼: 9.0771933

圖一:鏈接一

圖2:鏈接2


倫克:黑色

產品代碼: 9.0771734

圖一:鏈接一

圖2:鏈接2

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

我用這個代碼

它實際上有效但是: 第一個給出了第一部分的信息 以下僅顯示產品變體 它不再顯示第一個信息

此行有問題:

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

它從所有產品中選擇所有變體,而不僅僅是那些低於當前tu節點的變體。 將其更改為:

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

這將 select 節點相對於當前tu節點。

要select的圖片,可以使用如下代碼:

foreach (XmlNode picture in tuv.SelectNodes("picture"))
{
    Console.WriteLine("  " + picture.InnerText);
}

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM