当将其设置为null或空字符串的值时,在XElement上使用C#xmlSerializer和setElementValue时

发布于 2025-02-06 21:56:14 字数 249 浏览 1 评论 0原文

如果您使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏见 2025-02-13 21:56:16

抱歉,我花了一段时间才回到这个。我发现了我的问题是正确的。我之前没有注意到的是,这条线是在节省之前的结尾。

xDocument.Descendants().Where(e => string.IsNullOrEmpty(e.Value)).Remove();

这涉及到所有后代,发现任何零或空字符串并将其删除,这是我的问题。

XElement.SetElementValue(elementName, elementValue);

这完全按照记录。当元素值为null时,它将删除该元素,但是当它是一个空字符串时,它将将元素作为长形式的空元素留下,而不是短形式,而不是对我的情况很好。

为了使此答案的完整性,并且由于这些要求为例如代码,这里有一些。

sample.cfg

<?xml version="1.0" encoding="utf-8"?>
<ParentNode>
  <ChildNode>
    <PropertyOne>1</PropertyOne>
    <PropertyTwo>Y</PropertyTwo>
  </ChildNode>
  <ChildNode>
    <PropertyOne>2</PropertyOne>
    <PropertyTwo>N</PropertyTwo>
  </ChildNode>
</ParentNode>

示例代码

// See https://aka.ms/new-console-template for more information
using System.Xml.Linq;

var xDocument = XDocument.Load("Sample.cfg");

foreach (var childNode in xDocument.Descendants("ChildNode"))
{
    foreach (var element in childNode.Elements())
    {
        if (element.Name == "PropertyOne" && element.Value == "2")
        {
            childNode.SetElementValue("PropertyTwo", "");
        }

        // Uncomment this line to always have it remove null and empty string descendants
        //xDocument.Descendants().Where(e => string.IsNullOrEmpty(e.Value)).Remove();

        xDocument.Save("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.

xDocument.Descendants().Where(e => string.IsNullOrEmpty(e.Value)).Remove();

This goes through all the descendants and finds any that are null or empty strings and removes them which was my problem.

XElement.SetElementValue(elementName, elementValue);

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

<?xml version="1.0" encoding="utf-8"?>
<ParentNode>
  <ChildNode>
    <PropertyOne>1</PropertyOne>
    <PropertyTwo>Y</PropertyTwo>
  </ChildNode>
  <ChildNode>
    <PropertyOne>2</PropertyOne>
    <PropertyTwo>N</PropertyTwo>
  </ChildNode>
</ParentNode>

Sample Code

// See https://aka.ms/new-console-template for more information
using System.Xml.Linq;

var xDocument = XDocument.Load("Sample.cfg");

foreach (var childNode in xDocument.Descendants("ChildNode"))
{
    foreach (var element in childNode.Elements())
    {
        if (element.Name == "PropertyOne" && element.Value == "2")
        {
            childNode.SetElementValue("PropertyTwo", "");
        }

        // Uncomment this line to always have it remove null and empty string descendants
        //xDocument.Descendants().Where(e => string.IsNullOrEmpty(e.Value)).Remove();

        xDocument.Save("Sample.cfg");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文