2

我正在使用来自第三方来源的 XML,但似乎无法将 xml 正确加载到 XDocument 中。它将所有内容加载到一个元素中,并将其值设置为 xml 的其余部分。

xml 字符串看起来像这样(未格式化,从响应流中读取)。

<ArticleDetail xmlns="http://schemas.datacontract.org/2004/07/EEL.ArticleDatabase.WebService.Model" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Approved>false</Approved><ArticleGroupCode>5304</ArticleGroupCode><ArticleGroupName>Standblenders</ArticleGroupName><ArticleNumber>052ASB2600</ArticleNumber><BrandLogoTypeResourceId/><BrandName>Electrolux</BrandName><Denomination>Köksmaskiner ASB2600 Electrolux</Denomination><Description>Exklusiv mixer i borstat stål som enkelt krossar is, 1,6l glaskanna OBS! Levereras som 2-pack</Description><Documents/><Ean>7319590015596</Ean><Id>1649151</Id><Images/><ModelNumber>ASB2600</ModelNumber><PackageQuantity>1</PackageQuantity><Parameters><Parameter><Category>Dimensioner</Category><DataType>long</DataType><Description/><DisplayName>Bredd</DisplayName><Name>Bredd</Name><Priority>1</Priority><Unit>mm</Unit><Value>290</Value></Parameter><Parameter><Category>Dimensioner</Category><DataType>long</DataType><Description/><DisplayName>Djup</DisplayName><Name>Djup</Name><Priority>2</Priority><Unit>mm</Unit><Value>240</Value></Parameter><Parameter><Category>Dimensioner</Category><DataType>long</DataType><Description/><DisplayName>Höjd</DisplayName><Name>Höjd</Name><Priority>3</Priority><Unit>mm</Unit><Value>380</Value></Parameter></Parameters><Published>false</Published><ReplacedByArticleNumber/><ReplacesArticleNumber/><SellText1>Kraftfull - perfekt för att krossa is.</SellText1><SellText2>Glaskanna som rymmer 1,6 liter, med 6-bladigt knivsystem.</SellText2><SellText3>Väldigt enkel rengöring av kanna och knivsystem</SellText3><SellText4/><SellText5/><StatusCode>0</StatusCode><SupplierArticleNumber>ASB2600</SupplierArticleNumber><SupplierCode>150</SupplierCode><SupplierName>Elon Elkedjan Logistic AB</SupplierName><Texts><TextItem><Text>Test</Text><Type>Short</Type></TextItem><TextItem><Text>Elon</Text><Type>Long</Type></TextItem></Texts></ArticleDetail>

其余的代码看起来像这样。

var xDoc = XDocument.Parse(xmlString);

由于某种原因,所有内容都加载到一个元素中,并且值设置为。

false5304Standblenders052ASB2600ElectroluxKöksmaskiner ASB2600 ElectroluxExklusiv mixer i borstat stål som enkelt krossar is, 1,6l glaskanna OBS! Levereras som 2-pack73195900155961649151ASB26001DimensionerlongBreddBredd1mm290DimensionerlongDjupDjup2mm240DimensionerlongHöjdHöjd3mm380falseKraftfull - perfekt för att krossa is.Glaskanna som rymmer 1,6 liter, med 6-bladigt knivsystem.Väldigt enkel rengöring av kanna och knivsystem0ASB2600150Elon Elkedjan Logistic ABTestShortElonLong
4

1 回答 1

4

无法复制;它工作正常。我认为你读错了。例如,以下将节点名称绘制为树:

static void Main()
{
    string xmlString = @"<ArticleDetail xmlns=""http://schemas.datacontract.org/2004/07/EEL.ArticleDatabase.WebService.Model"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><Approved>false</Approved><ArticleGroupCode>5304</ArticleGroupCode><ArticleGroupName>Standblenders</ArticleGroupName><ArticleNumber>052ASB2600</ArticleNumber><BrandLogoTypeResourceId/><BrandName>Electrolux</BrandName><Denomination>Köksmaskiner ASB2600 Electrolux</Denomination><Description>Exklusiv mixer i borstat stål som enkelt krossar is, 1,6l glaskanna OBS! Levereras som 2-pack</Description><Documents/><Ean>7319590015596</Ean><Id>1649151</Id><Images/><ModelNumber>ASB2600</ModelNumber><PackageQuantity>1</PackageQuantity><Parameters><Parameter><Category>Dimensioner</Category><DataType>long</DataType><Description/><DisplayName>Bredd</DisplayName><Name>Bredd</Name><Priority>1</Priority><Unit>mm</Unit><Value>290</Value></Parameter><Parameter><Category>Dimensioner</Category><DataType>long</DataType><Description/><DisplayName>Djup</DisplayName><Name>Djup</Name><Priority>2</Priority><Unit>mm</Unit><Value>240</Value></Parameter><Parameter><Category>Dimensioner</Category><DataType>long</DataType><Description/><DisplayName>Höjd</DisplayName><Name>Höjd</Name><Priority>3</Priority><Unit>mm</Unit><Value>380</Value></Parameter></Parameters><Published>false</Published><ReplacedByArticleNumber/><ReplacesArticleNumber/><SellText1>Kraftfull - perfekt för att krossa is.</SellText1><SellText2>Glaskanna som rymmer 1,6 liter, med 6-bladigt knivsystem.</SellText2><SellText3>Väldigt enkel rengöring av kanna och knivsystem</SellText3><SellText4/><SellText5/><StatusCode>0</StatusCode><SupplierArticleNumber>ASB2600</SupplierArticleNumber><SupplierCode>150</SupplierCode><SupplierName>Elon Elkedjan Logistic AB</SupplierName><Texts><TextItem><Text>Test</Text><Type>Short</Type></TextItem><TextItem><Text>Elon</Text><Type>Long</Type></TextItem></Texts></ArticleDetail>";
    var xDoc = XDocument.Parse(xmlString);
    Write(xDoc.Root, 0);
}

static void Write(XElement el, int offset)
{
    Console.Write(new string(' ', offset));
    Console.WriteLine(el.Name.LocalName);
    foreach (var child in el.Elements())
    {
        Write(child, offset + 1);
    }
}

我怀疑您正在访问xDoc.Root.Value- 实际上,您显示的根的组合值;但这并不意味着结构缺失。如果那不是您想要的,那么不要问是否需要该值-而是走结构。

于 2012-08-09T09:28:45.360 回答