我正在尝试生成这样的XML:
<?xml version="1.0"?>
<!DOCTYPE APIRequest SYSTEM
"https://url">
<APIRequest>
<Head>
<Key>123</Key>
</Head>
<ObjectClass>
<Field>Value</Field
</ObjectClass>
</APIRequest>
我有一个用XMLSerialization属性装饰的类(ObjectClass),如下所示:
[XmlRoot("ObjectClass")]
public class ObjectClass
{
[XmlElement("Field")]
public string Field { get; set; }
}
我的真正hacky直觉思想让我的工作就是在序列化时这样做:
ObjectClass inst = new ObjectClass();
XmlSerializer serializer = new XmlSerializer(inst.GetType(), "");
StringWriter w = new StringWriter();
w.WriteLine(@"<?xml version=""1.0""?>");
w.WriteLine("<!DOCTYPE APIRequest SYSTEM");
w.WriteLine(@"""https://url"">");
w.WriteLine("<APIRequest>");
w.WriteLine("<Head>");
w.WriteLine(@"<Field>Value</Field>");
w.WriteLine(@"</Head>");
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
serializer.Serialize(w, inst, ns);
w.WriteLine("</APIRequest>");
但是,这会生成如下XML:
<?xml version="1.0"?>
<!DOCTYPE APIRequest SYSTEM
"https://url">
<APIRequest>
<Head>
<Key>123</Key>
</Head>
<?xml version="1.0" encoding="utf-16"?>
<ObjectClass>
<Field>Value</Field>
</ObjectClass>
</APIRequest>
即。 serialize语句自动添加&lt;?xml文本声明。
我知道我正在攻击这个错误,所以有人能指出我正确的方向吗?
作为一个注释,我认为只使用一个带有ObjectClass的APIRequest类来实现它是否具有实际意义(因为有20种不同类型的ObjectClass,它们每个都需要这个样板)但是如果我错了。
答案 0 :(得分:30)
试试这个:
internal static string ToXml(object obj)
{
string retval = null;
if (obj != null)
{
StringBuilder sb = new StringBuilder();
using(XmlWriter writer = XmlWriter.Create(sb, new XmlWriterSettings() { OmitXmlDeclaration = true }))
{
new XmlSerializer(obj.GetType()).Serialize(writer, obj);
}
retval = sb.ToString();
}
return retval;
}
答案 1 :(得分:24)
永远不要使用字符串连接来构建xml。这是邪恶的。
输出:
<?xml version="1.0" encoding="utf-16"?>
<!DOCTYPE APIRequest SYSTEM "https://url">
<APIRequest>
<Head>
<Key>123</Key>
</Head>
<ObjectClass>
<Field>Value</Field>
</ObjectClass>
</APIRequest>
代码:
using System;
using System.Diagnostics;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
public static class Program {
public static void Main() {
var obj = new ObjectClass { Field = "Value" };
var settings = new XmlWriterSettings {
Indent = true
};
var xml = new StringBuilder();
using (var writer = XmlWriter.Create(xml, settings)) {
Debug.Assert(writer != null);
writer.WriteDocType("APIRequest", null, "https://url", null);
writer.WriteStartElement("APIRequest");
writer.WriteStartElement("Head");
writer.WriteElementString("Key", "123");
writer.WriteEndElement(); // </Head>
var nsSerializer = new XmlSerializerNamespaces();
nsSerializer.Add("", "");
var xmlSerializer = new XmlSerializer(obj.GetType(), "");
xmlSerializer.Serialize(writer, obj, nsSerializer);
writer.WriteEndElement(); // </APIRequest>
}
Console.WriteLine(xml.ToString());
Console.ReadLine();
}
}
[XmlRoot("ObjectClass")]
public class ObjectClass {
[XmlElement("Field")]
public string Field { get; set; }
}
答案 2 :(得分:2)
如果出于性能原因不想依赖xml编写器等,可以这样做:
// Read into memory stream and set namespaces to empty strings
XmlSerializerNamespaces nsSerializer = new XmlSerializerNamespaces();
nsSerializer.Add(string.Empty, string.Empty);
XmlSerializer xs = new XmlSerializer(typeof(Model.AudioItem));
xs.Serialize(ms, item, nsSerializer);
// Read into UTF-8 stream and read off first line (i.e "<?xml version="1.0"?>")
StreamReader sr = new StreamReader(ms);
ms.Position = 0;
sr.ReadLine();
sr.ReadToEnd()。ToString()现在包含裸序列化
答案 3 :(得分:1)
派生您自己的XmlTextWriter以省略XML声明。
Private Class MyXmlTextWriter
Inherits XmlTextWriter
Sub New(ByVal sb As StringBuilder)
MyBase.New(New StringWriter(sb))
End Sub
Sub New(ByVal w As TextWriter)
MyBase.New(w)
End Sub
Public Overrides Sub WriteStartDocument()
' Don't emit XML declaration
End Sub
Public Overrides Sub WriteStartDocument(ByVal standalone As Boolean)
' Don't emit XML declaration
End Sub
End Class
使用派生的MyXmlTextWriter的实例调用Serialize。
Dim tw As New MyXmlTextWriter(sb)
Dim objXmlSerializer As New XmlSerializer(type)
objXmlSerializer.Serialize(tw, obj)
答案 4 :(得分:1)
Scott Hanselman's在此发表了一篇好文章。我使用了Kzu的例子(斯科特的博客指向的)一段时间以后也做了同样的事情并且效果很好。
答案 5 :(得分:-1)
if (!string.IsNullOrEmpty(strXML) && strXML.Contains(@"<?xml"))
strXML = strXML.Remove(0, strXML.IndexOf(@"?>", 0) + 2);
答案 6 :(得分:-2)
一条衬里,用于从字符串中删除第一行:
String.Join("\n", strXML.Split('\n').Skip(1).ToArray())
不优雅,但简洁。