gpt4 book ai didi

c# - 是否可以缩短 XDocument 的 XDeclaration?

转载 作者:太空宇宙 更新时间:2023-11-03 10:43:50 24 4
gpt4 key购买 nike

我正在向我的 Xdocument 添加一个 XDeclaration,如下所示:

XDocument xml = new XDocument(new XDeclaration("1.0", "utf-8", "yes")

这给了我结果:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>

我正在为 Quickbooks Merchant Services 构建 XML,所以我只需要:

<?xml version="1.0"?>

这有可能吗?

最佳答案

您可以尝试使用不提供任何编码的自定义 StringWriter,例如:

public class StringWriterNoEncoding : StringWriter
{
public override Encoding Encoding
{
get { return null; }
}
}

然后使用 StringWriterNoEncoding 打印 XML:

XDocument xdoc = new XDocument(new XDeclaration("1.0", "", ""), new XElement("Root"));
var sw = new StringWriterNoEncoding();
xdoc.Save(sw);
Console.WriteLine(sw.ToString());
//above will print :
//<?xml version="1.0"?>
//<Root />

关于c# - 是否可以缩短 XDocument 的 XDeclaration?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24253150/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com