我想在 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/