c# - 如何使用 XmlDocument/XmlDeclaration 添加自定义 XmlDeclaration?

标签 c# .net xml xmldocument

我想在 c# .net 2 或 3 中使用 XmlDocument/XmlDeclaration 类时创建自定义 XmlDeclaration。

这是我想要的输出(这是第三方应用程序的预期输出):

<?xml version="1.0" encoding="ISO-8859-1" ?>
<?MyCustomNameHere attribute1="val1" attribute2="val2" ?>
[ ...more xml... ]

使用 XmlDocument/XmlDeclaration 类,看来我只能创建一个具有一组已定义参数的 XmlDeclaration:

XmlDocument doc = new XmlDocument();
XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "ISO-8859-1", null);
doc.AppendChild(declaration);

除了 XmlDocument/XmlDeclaration 之外,是否还有其他类可供我查看以创建自定义 XmlDeclaration?或者有没有办法使用 XmlDocument/XmlDeclaration 类本身?

最佳答案

您要创建的不是 XML 声明,而是“处理指令”。你应该使用 XmlProcessingInstruction类,而不是 XmlDeclaration类,例如:

XmlDocument doc = new XmlDocument();
XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "ISO-8859-1", null);
doc.AppendChild(declaration);
XmlProcessingInstruction pi = doc.CreateProcessingInstruction("MyCustomNameHere", "attribute1=\"val1\" attribute2=\"val2\"");
doc.AppendChild(pi);

关于c# - 如何使用 XmlDocument/XmlDeclaration 添加自定义 XmlDeclaration?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/334256/

相关文章:

C# 自动清除 Winform 文本框

c# - 透明选项卡控件显示为白色

c# - 将 azure 数据库连接到现有的 xamarin 项目

c# - 如何使异步事件连续运行?

c# - 带有大量 NUnit 测试的开源 .NET 应用程序项目(不是框架或工具项目)

c# - Linq 不包含动态查询

c# - 如何附加到表达式

java - URI 的引用没有使用 Apache Santuario 的 XMLSignatureInput

iphone - 如何检查连接到互联网的 url?

xml - Swift NSXML 需要循环来创建多个子项