通过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 Code
、Color
、Variant Code
和 Picture
的产品
我想先获取 Product_code
,然后获取 Product_code
的所有 Variant
例如:
Product_code: 9.077
价格2: 799.99
伦克:白
产品代码: 9.0771933
图片1:链接1
图片2:链接2
伦克:黑色
产品代码: 9.0771734
图片1:链接1
图片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"))
It selects all 所有产品的变体,而不仅仅是当前 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; }
}
}
<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 Code
、Color
、Variant Code
和 Picture
我想先获取 Product_code
,然后获取 Product_code
Variant
例如:
Product_code: 9.077
价格2: 799.99
伦克:白
产品代码: 9.0771933
图片1:链接1
图片2:链接2
伦克:黑色
产品代码: 9.0771734
图片1:链接1
图片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"))
It selects all 所有产品的变体,而不仅仅是当前 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; }
}
}