【问题标题】:create XMLDocument that have not any document root node [closed]创建没有任何文档根节点的 XMLDocument [关闭]
【发布时间】:2018-10-10 13:55:07
【问题描述】:

我想创建一个类似的xml。

<?xml version="1.0" encoding="utf-8"?>   
<Initials>ANP</Initials>
<MedicalPracticeName>Dr. Anny</MedicalPracticeName>
<Start>2016-11-17</Start>
<Logon>
   <UserId>admin</UserId>
   <Password>abcde</Password>
</Logon>

通过使用 XMLDocument 类。

我试过这段代码,但没有成功

XmlDocument xmlDoc = new XmlDocument();
XmlNode firstNode = xmlDoc.CreateNode(XmlNodeType.Element,"Initials","http://www.google.com");
xmlDoc.AppendChild(firstNode );

firstNode.InnerText = "ANP";

XmlNode SecondNode = xmlDoc.CreateNode(XmlNodeType.Element, "MedicalPracticeName", "http://www.google.com");
xmlDoc.AppendChild(SecondNode);

SecondNode.InnerText = "Dr. Anny";

XmlNode ThirdNode = xmlDoc.CreateNode(XmlNodeType.Element, "Start", "http://www.google.com");
xmlDoc.AppendChild(ThirdNode );

ThirdNode.InnerText = "2016-11-17";

【问题讨论】:

  • 您的 Xml 样本格式不正确。你为什么要做这样的事情?
  • 你不能。一个有效的 Xml 文档恰好需要 1 个根元素。
  • 我理解 XMLDocument 必须只有一个根节点。但是任何其他方式,如何创建上述 XML。比如将不同的 XMLNode 连接到一个 XMLDocument。

标签: c# xmldocument


【解决方案1】:

您的代码看起来不错,您的问题在于 XML 本身,您应该遵循一些标准,或者尽可能使用其他格式。如果您不遵循它,您或其他任何人在将来使用这些文件时可能会遇到一些麻烦。

您可以查看this questionW3C specifications

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-09
    • 2019-06-17
    • 2012-07-14
    • 1970-01-01
    • 2012-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多