我发现本指南中,我的XML文件转换为CSV文本:https://msdn.microsoft.com/en-us/library/mt693157.aspx具有相同名称的转换节点用C#
一切安好,直到节点的部分具有相同的名称
<ProductSpecification>
<Property>
<Name>CLEANING PERFORMANCE</Name>
<Value>-</Value>
</Property>
<Property>
<Name>COLOUR</Name>
<Value>White</Value>
</Property>
<Property>
<Name>DIMENSIONS (H x W x D cm)</Name>
<Value>85 x 60 x 45</Value>
</Property>
<Property>
<Name>DISH SETTING CAPACITY</Name>
<Value>-</Value>
</Property>
</ProductSpecification>
如果我尝试使用
(string)el.Element("ProductSpecification"),
我的整个文本中一个大的一堆字母组合没有节点之间的空格:
CLEANING PERFORMANCE-COLOURWhiteDIMENSIONS (H x W x D cm)85 x 60 x 45DISH SETTING CAPACITY-
如果我尝试使用
(string)el.Element("ProductSpecification").Element("Property"),
我只是得到一个错误没有的什么是错的
,我需要的是结果任何解释:
Cleaning Performance
-
Colour
White
洙.. 。我尝试了所有我能想到的东西,但无法用节点之间的空格编写文本 编辑:在时刻添加我的整个脚本:
XElement custOrd = XElement.Load("_output.xml");
string csv =
(from el in custOrd.Elements("PriceCatalog").Elements("ListofCatalogDetails").Elements("CatalogItem").Elements("Product").Elements("ProductSpec").Elements("ProductDetails")
select
String.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14},{15},{16}, \"{17}\"{18}",
(string)el.Element("ProductID"),
(string)el.Element("PartNumber"),
(string)el.Element("Description"),
(string)el.Element("LongDesc"),
(string)el.Element("Manufacturer"),
(string)el.Element("Category01"),
(string)el.Element("Category02"),
(string)el.Element("Category03"),
(string)el.Element("CategoryVAT"),
(string)el.Element("PeriodofWarranty"),
(string)el.Element("Qty").Element("QtyAvailable"),
(string)el.Element("UnitPrice"),
(string)el.Element("VatRate"),
(string)el.Element("Weight"),
(string)el.Element("Height"),
(string)el.Element("Width"),
(string)el.Element("Length"),
(string)el.Element("ProductSpecification"),
Environment.NewLine
)
)
.Aggregate(
new StringBuilder(),
(sb, s) => sb.Append(s),
sb => sb.ToString()
);
Console.WriteLine(csv);
EDIT02:我刚刚发现有些项目不具备“ProductSpecification”节点,但是C#不提供有关的任何错误,但如果我用“ProductSpecification.Property” - 它崩溃。现在我只需要绕过这个莫名其妙
而具体的错误信息是? –
使用“元素(”属性“)”不是“元素(”属性“)”(字符串)el.Element(“ProductSpecification”)。元素(“属性”) – Vijai
Luc,我得到这个:'System.NullReferenceException未处理 HResult = -2147467261 Message =对象引用未设置为对象的实例。# – tdesws