当将其设置为null或空字符串的值时,在XElement上使用C#xmlSerializer和setElementValue时
如果您使用Xdocument..load(路径)打开XML文档,然后在找到正在寻找的XML文档(路径)时浏览后代,如果将值设置为空字符串(“”)或NULL时,请使用SetElementValue或使用SetElementValue。标记,因此,当您保存文档时,它会丢失。
当值为空或一个空字符串时,我希望能够保留标签。我无法找到这样做的方法。
我的唯一选择将整个XML文档列为对象编辑这些对象并在文件上写入,而不仅仅是加载xmldocument并进行编辑?
If you open an XML Document with XDocument.Load(path) and then look through Descendants when you find the one you are looking for and use SetElementValue if you set the value to an empty string ("") or null it ends up removing the tag so when you save the document it's lost.
I want to be able to keep the tag when the value is null or an empty string. I've not been able to find a way to do this.
Is my only option to deserialize the entire XML document into objects edit those objects and write over the file rather than just loading the XmlDocument and editing it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抱歉,我花了一段时间才回到这个。我发现了我的问题是正确的。我之前没有注意到的是,这条线是在节省之前的结尾。
这涉及到所有后代,发现任何零或空字符串并将其删除,这是我的问题。
这完全按照记录。当元素值为null时,它将删除该元素,但是当它是一个空字符串时,它将将元素作为长形式的空元素留下,而不是短形式,而不是对我的情况很好。
为了使此答案的完整性,并且由于这些要求为例如代码,这里有一些。
sample.cfg
示例代码
Sorry, it took me a while to get back to this. I found what my issue was so the code was all correct. What I hadn't noticed before was that this line was at the end before the save.
This goes through all the descendants and finds any that are null or empty strings and removes them which was my problem.
This does exactly as documented. When the elementValue is NULL it will remove the element but when it's an empty string it will put leave the element as an empty element in long-form, not the short form which is fine for my case.
For completeness of this answer and since those asked for example code here is some.
Sample.cfg
Sample Code