XML字符串或对象序列化?

时间:2017-12-18 12:41:12

标签: c# xml

像字符串一样创建XML元素是否合适?例如:

string xmlString = $@"<methodCall>
<methodName>c.mail</methodName>
<params>
<param><value><string>{number}</string></value></param>
<param><value><string>{email}</string></value></param>
<param><value><i4>5</i4></value></param>
</params>
</methodCall>";

并加载

doc.Load(new StringReader(xmlString));
XmlDeclaration xmlDec;
xmlDec = doc.CreateXmlDeclaration("1.0", null, null);
xmlDec.Encoding = "UTF-8";

XmlElement root = doc.DocumentElement;
doc.InsertBefore(xmlDec, root);

(灵感来自MS:https://msdn.microsoft.com/de-de/library/system.xml.xmldeclaration.encoding%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

或者这可能导致任何问题,我应该创建一个对象并序列化这个?什么时候,怎么样?由于只使用一个简单的字符串非常快,我更喜欢这个解决方案 - 但如果这是不好的做法或者可能导致问题,那就不行了。 (XML对我来说是新的)

编辑:

为什么需要更多细节?我想我的问题很明确? 对于有兴趣的人我是如何解决这个问题的。感谢指南,我将代码更改为:

#region root Node
XmlDocument xmlDoc = new XmlDocument();
XmlNode rootNode = xmlDoc.CreateElement("methodCall");
xmlDoc.AppendChild(rootNode);
#endregion

#region XML Node Method name
XmlNode methodNameNode = xmlDoc.CreateElement("methodName");
methodNameNode.InnerText = "customer.sendmail";
rootNode.AppendChild(methodNameNode);
#endregion


#region XML Parameters
XmlNode paramsNode = xmlDoc.CreateElement("params");

#region Setting Number
XmlNode paramElement = xmlDoc.CreateElement("param");
XmlNode valueAttribute = xmlDoc.CreateElement("value");
XmlNode stringAttribute = xmlDoc.CreateElement("string");
stringAttribute.InnerText = number;
valueAttribute.AppendChild(stringAttribute);
paramElement.AppendChild(valueAttribute);
#endregion

#region Setting Email
XmlNode paramElement2 = xmlDoc.CreateElement("param");
XmlNode valueAttribute2 = xmlDoc.CreateElement("value");
XmlNode stringAttribute2 = xmlDoc.CreateElement("string");
stringAttribute2.InnerText = email;
valueAttribute2.AppendChild(stringAttribute2);
paramElement2.AppendChild(valueAttribute2);
#endregion

#region Setting validation
XmlNode paramElement3 = xmlDoc.CreateElement("param");
XmlNode valueAttribute3 = xmlDoc.CreateElement("value");
XmlNode stringAttribute3 = xmlDoc.CreateElement("i4");
stringAttribute3.InnerText = "5";
valueAttribute3.AppendChild(stringAttribute3);
paramElement3.AppendChild(valueAttribute3);
#endregion

#region Adding ISC, Email and vadlidation to paramsNode
paramsNode.AppendChild(paramElement);
paramsNode.AppendChild(paramElement2);
paramsNode.AppendChild(paramElement3);
rootNode.AppendChild(paramsNode);
#endregion

#endregion

#region Setting encoding to UTF8
XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", null, null);
xmlDeclaration.Encoding = "UTF-8";
//xmlDec.Standalone = "yes";
XmlElement root = xmlDoc.DocumentElement;
xmlDoc.InsertBefore(xmlDeclaration, root);
#endregion

这将给出(例如)以下XML:

XML example

但我真的可以推荐阅读教程:http://csharp.net-tutorials.com/xml/writing-xml-with-the-xmlwriter-class/

了解XML非常有帮助。

1 个答案:

答案 0 :(得分:4)

自己做这件事很糟糕,不管你如何转身看看它。

示例:如果您的字段编号或电子邮件包含需要在XML中转义的字符,该怎么办?哎呀,坏了。所以不要手工完成。获得一个工具。在.NET中,XML序列化非常简单,你不应该考虑它。

然后手动构建它,然后使用序列化程序对其进行反序列化并将其放入序列化树中?那是愚蠢的。有一个序列化器可用,至少用它来构建XML。你不需要类,但至少用它来编写节点和元素,摆脱格式化并以这种方式逃避问题。