[英]Concatenating XML Element Values to String using XDocument
Given an XML Document - for instance: 给定一个XML文档-例如:
<factory>
<widgets>
<widget>Foo</widget>
<widget>Bar</widget>
<widget>Baz</widget>
<widget>Qux</widget>
</widgets>
</factory>
I wish to build a line-break separated string of widget values - using the above XML, this would be: 我希望构建一个用换行符分隔的小部件值字符串-使用上述XML,它将是:
Foo
Bar
Baz
Qux
The code I'm using to do this is: 我用于执行此操作的代码是:
var doc = XDocument.Parse(xml) //where XML is a string containing the above XML
var builder = new StringBuilder();
foreach(var widget in doc.Root.Element("widgets").Elements("widget"))
{
builder.AppendLine(widget.Value);
}
However, the resulting string is FooBarBazQux
rather than a newline-separated version. 但是,结果字符串是
FooBarBazQux
而不是换行符分隔的版本。 Setting a breakpoint on the AppendLine call reveals that widget.Value
is being set to "FooBarBazQux", and the loop runs once rather than the 4 times I'm expecting. 在AppendLine调用上设置断点将显示
widget.Value
设置为“ FooBarBazQux”,并且循环运行一次,而不是我期望的4次。
I've tried running the query in LinqPad: 我试过在LinqPad中运行查询:
XDocument settings = XDocument.Parse (@"
<factory>
<widgets>
<widget>Foo</widget>
<widget>Bar</widget>
<widget>Baz</widget>
<widget>Qux</widget>
</widgets>
</factory>");
foreach(var x in settings.Root.Elements("widgets").Elements("widget"))
x.Value.Dump("Widget Type");
and the results are correct and as expected. 结果正确且符合预期。
Can anyone help me in getting a newline separated string of Widget
values? 谁能帮助我获取以换行符分隔的
Widget
值字符串? I'm at a bit of a loss! 我有点茫然!
It might be worth noting that this is within a Xamarin.Forms application, using the PCL version of using System.Xml.Linq. 可能值得注意的是,该文件位于Xamarin.Forms应用程序中,使用的是使用System.Xml.Linq的PCL版本。
I don't know whether I should add it as an answer so it can be marked as such (or if I will get shouted at because I have already put it in a comment) 我不知道是否应该将其添加为答案,以便可以将其标记为这样(或者因为我已经在评论中添加它而大喊大叫)
builder.ToString() builder.ToString()
This was a stupid mistake, caused by "Fast Deployment" being enabled in the Android Xamarin project. 这是一个愚蠢的错误,原因是在Android Xamarin项目中启用了“快速部署”。
My XML File was being generated by the app if it didn't exist - I assumed that on re-deploying, all app assets were to being removed from the device. 我的XML文件是由应用程序生成的(如果不存在)-我假设重新部署时,所有应用程序资产都将从设备中删除。 It turns out that when Fast Deployment is enabled, the device only updates NEW components of the application - ie the XML file wasn't being removed.
事实证明,启用快速部署后,设备仅更新应用程序的新组件-即未删除XML文件。 The problem was caused by the XML file being out of date.
该问题是由XML文件过期引起的。
I believe that the XML document being initially incorrect was caused by the potential issue that StuartLC pointed out - I must have at one point called builder.AppendLine(doc.Root.Element("widgets").Value)
, which returns the concatenation of the value of all child elements of widgets. 我认为最初不正确的XML文档是由StuartLC指出的潜在问题引起的-我必须在某一时刻具有
builder.AppendLine(doc.Root.Element("widgets").Value)
,它返回的串联小部件的所有子元素的值。
Thanks for all the help! 感谢您的所有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.