我从第三方来源使用XML,但是似乎无法将xml正确加载到XDocument中。它将所有内容加载到一个元素中,并将其值设置为xml的其余部分。
xml字符串如下所示(未格式化,从responsestream中读取)。
<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
最佳答案
无法复制;它工作正常。我认为您读错了。例如,以下内容将节点名称绘制为树:
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
-实际上,您显示的是根的组合值;但这并不意味着结构丢失。如果这不是您想要的,请不要问是否要提供该值-而是遍历结构。关于c# - 使用XDocument.Parse将所有内容加载到一个元素中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11880259/