如何选择当前节点的所有子节点?
XmlNodeList productNodes = doc.DocumentElement.SelectNodes("//PRODUCT");
Parallel.ForEach(productNodes.Cast<XmlNode>(),
(XmlNode productNode) =>
{
string _productMode = XmlUtils.nodeAsString(productNode, "@mode");
Product product = new Product()
{
Mode = XmlUtils.nodeAsString(productNode, "@mode"),
No = XmlUtils.nodeAsString(productNode, "./SUPPLIER_PID"),
DescriptionShort = XmlUtils.nodeAsString(productNode, "./PRODUCT_DETAILS/DESCRIPTION_SHORT"),
DescriptionLong = XmlUtils.nodeAsString(productNode, "./PRODUCT_DETAILS/DESCRIPTION_LONG"),
EANCode = XmlUtils.nodeAsString(productNode, "./PRODUCT_DETAILS/EAN"),
Stock = XmlUtils.nodeAsInt(productNode, "./PRODUCT_DETAILS/STOCK"),
OrderUnit = default(QuantityCodes).FromString(XmlUtils.nodeAsString(productNode, "./PRODUCT_ORDER_DETAILS/ORDER_UNIT")),
ContentUnit = default(QuantityCodes).FromString(XmlUtils.nodeAsString(productNode, "./PRODUCT_ORDER_DETAILS/CONTENT_UNIT")),
Currency = default(CurrencyCodes).FromString(XmlUtils.nodeAsString(productNode, "./PRODUCT_PRICE_DETAILS/PRODUCT_PRICE/PRICE_CURRENCY")),
VAT = XmlUtils.nodeAsInt(productNode, "./PRODUCT_PRICE_DETAILS/PRODUCT_PRICE/TAX"),
};
XmlNodeList MimeNodes = doc.DocumentElement.SelectNodes(".//MIME");
Parallel.ForEach(MimeNodes.Cast<XmlNode>(), (XmlNode MimeNode) =>
{
Mime mime = new Mime()
{
Source = XmlUtils.nodeAsString(MimeNode, "./MIME_SOURCE"),
Type = XmlUtils.nodeAsString(MimeNode, "./MIME_TYPE"),
Purpose = XmlUtils.nodeAsString(MimeNode, "./MIME_PURPOSE")
};
product.Mime.Add(mime);
});......
<PRODUCT mode="new">
<SUPPLIER_PID>1902</SUPPLIER_PID>
<PRODUCT_DETAILS>
<DESCRIPTION_SHORT>NORDEN Table</DESCRIPTION_SHORT>
<DESCRIPTION_LONG>massive birch wood. 135x74cm, 74 cm height by Mikael Warnhammar</DESCRIPTION_LONG>
</PRODUCT_DETAILS>
<PRODUCT_ORDER_DETAILS>
<ORDER_UNIT>C62</ORDER_UNIT>
<CONTENT_UNIT>C62</CONTENT_UNIT>
<NO_CU_PER_OU>1</NO_CU_PER_OU>
</PRODUCT_ORDER_DETAILS>
<PRODUCT_PRICE_DETAILS>
<VALID_START_DATE>2013-05-28</VALID_START_DATE>
<VALID_END_DATE>2013-05-30</VALID_END_DATE>
<PRODUCT_PRICE price_type="nrp">
<PRICE_AMOUNT>33.00</PRICE_AMOUNT>
<PRICE_CURRENCY>EUR</PRICE_CURRENCY>
<LOWER_BOUND>200</LOWER_BOUND>
<TERRITORY>AT</TERRITORY>
</PRODUCT_PRICE>
</PRODUCT_PRICE_DETAILS>
<MIME>
<MIME_SOURCE>http://www.google.de/</MIME_SOURCE>
<MIME_TYPE>text/html</MIME_TYPE>
<MIME_PURPOSE>url</MIME_PURPOSE>
</MIME>
<MIME>
<MIME_SOURCE>http://www.stackoverflow.com</MIME_SOURCE>
<MIME_TYPE>text/html</MIME_TYPE>
<MIME_PURPOSE>url</MIME_PURPOSE>
</MIME>
</PRODUCT>
实际上我得到了所有产品节点,我试图获取每个产品节点的所有mimes子节点来保存它..但是当我这样做时,我重复了mime节点。
我不太了解xpath可能是因为我需要speficy祖先节点?我尝试使用后代,但我得到了相同的结果。
答案 0 :(得分:0)
尝试使用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<XElement> productNodes = doc.Descendants("PRODUCT").ToList();
List<Product> product = productNodes.Select(x => new Product() {
Mode = (string)x.Attribute("mode"),
No = (string)x.Element("SUPPLIER_PID"),
DescriptionShort = (string)x.Descendants("DESCRIPTION_SHORT").FirstOrDefault(),
DescriptionLong = (string)x.Descendants("DESCRIPTION_LONG").FirstOrDefault(),
//EANCode = XmlUtils.nodeAsString(productNode, "./PRODUCT_DETAILS/EAN"),
//Stock = XmlUtils.nodeAsInt(productNode, "./PRODUCT_DETAILS/STOCK"),
OrderUnit = (string)x.Descendants("ORDER_UNIT").FirstOrDefault(),
ContentUnit = (string)x.Descendants("CONTENT_UNIT").FirstOrDefault(),
Currency = (string)x.Descendants("PRICE_CURRENCY").FirstOrDefault(),
mime = x.Elements("MIME").Select(y => new Mime() {
Source = (string)y.Element("MIME_SOURCE"),
Type = (string)y.Element("MIME_TYPE"),
Purpose = (string)y.Element("MIME_PURPOSE")
}).ToList()
}).ToList();
}
}
public class Product
{
public string Mode { get; set;}
public string No { get; set;}
public string DescriptionShort { get; set;}
public string DescriptionLong { get; set;}
public string EANCode { get; set;}
public string OrderUnit { get; set;}
public string ContentUnit { get; set;}
public string Currency { get; set;}
public string Vat { get; set;}
public List<Mime> mime { get; set;}
}
public class Mime
{
public string Source { get; set; }
public string Type { get; set;}
public string Purpose { get; set;}
}
}