C# XmlDocument 节点

发布于 2024-07-26 18:25:25 字数 2074 浏览 3 评论 0原文

我正在尝试访问 UPS 跟踪信息,根据他们的示例,我需要构建一个如下请求:

<?xml version="1.0" ?>
<AccessRequest xml:lang='en-US'>
   <AccessLicenseNumber>YOURACCESSLICENSENUMBER</AccessLicenseNumber>
   <UserId>YOURUSERID</UserId>
   <Password>YOURPASSWORD</Password>
</AccessRequest>
<?xml version="1.0" ?>
<TrackRequest>
   <Request>
     <TransactionReference>
         <CustomerContext>guidlikesubstance</CustomerContext>
     </TransactionReference>
     <RequestAction>Track</RequestAction>
   </Request>
   <TrackingNumber>1Z9999999999999999</TrackingNumber>
</TrackRequest>

我在使用 C# 中的 1 XmlDocument 创建此请求时遇到问题。 当我尝试添加第二个时: 它抛出一个错误:

System.InvalidOperationException:这个 文档已经有一个 “DocumentElement”节点。

我猜测这是因为标准 XmlDocument 只有 1 个根节点。 有任何想法吗?

到目前为止我的代码如下:

XmlDocument xmlDoc = new XmlDocument();
XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
XmlElement rootNode = xmlDoc.CreateElement("AccessRequest");
rootNode.SetAttribute("xml:lang", "en-US");
xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);
xmlDoc.AppendChild(rootNode);

XmlElement licenseNode = xmlDoc.CreateElement("AccessLicenseNumber");
XmlElement userIDNode = xmlDoc.CreateElement("UserId");
XmlElement passwordNode = xmlDoc.CreateElement("Password");

XmlText licenseText = xmlDoc.CreateTextNode("mylicense");
XmlText userIDText = xmlDoc.CreateTextNode("myusername");
XmlText passwordText = xmlDoc.CreateTextNode("mypassword");

rootNode.AppendChild(licenseNode);
rootNode.AppendChild(userIDNode);
rootNode.AppendChild(passwordNode);

licenseNode.AppendChild(licenseText);
userIDNode.AppendChild(userIDText);
passwordNode.AppendChild(passwordText);

XmlElement rootNode2 = xmlDoc.CreateElement("TrackRequest");
xmlDoc.AppendChild(rootNode2);

I'm trying to access UPS tracking info and, as per their example, I need to build a request like so:

<?xml version="1.0" ?>
<AccessRequest xml:lang='en-US'>
   <AccessLicenseNumber>YOURACCESSLICENSENUMBER</AccessLicenseNumber>
   <UserId>YOURUSERID</UserId>
   <Password>YOURPASSWORD</Password>
</AccessRequest>
<?xml version="1.0" ?>
<TrackRequest>
   <Request>
     <TransactionReference>
         <CustomerContext>guidlikesubstance</CustomerContext>
     </TransactionReference>
     <RequestAction>Track</RequestAction>
   </Request>
   <TrackingNumber>1Z9999999999999999</TrackingNumber>
</TrackRequest>

I'm having a problem creating this with 1 XmlDocument in C#. When I try to add the second:
<?xml version="1.0" ?> or the <TrackRequest>
it throws an error:

System.InvalidOperationException: This
document already has a
'DocumentElement' node.

I'm guessing this is because a standard XmlDocument would only have 1 root node. Any ideas?

Heres my code so far:

XmlDocument xmlDoc = new XmlDocument();
XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
XmlElement rootNode = xmlDoc.CreateElement("AccessRequest");
rootNode.SetAttribute("xml:lang", "en-US");
xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);
xmlDoc.AppendChild(rootNode);

XmlElement licenseNode = xmlDoc.CreateElement("AccessLicenseNumber");
XmlElement userIDNode = xmlDoc.CreateElement("UserId");
XmlElement passwordNode = xmlDoc.CreateElement("Password");

XmlText licenseText = xmlDoc.CreateTextNode("mylicense");
XmlText userIDText = xmlDoc.CreateTextNode("myusername");
XmlText passwordText = xmlDoc.CreateTextNode("mypassword");

rootNode.AppendChild(licenseNode);
rootNode.AppendChild(userIDNode);
rootNode.AppendChild(passwordNode);

licenseNode.AppendChild(licenseText);
userIDNode.AppendChild(userIDText);
passwordNode.AppendChild(passwordText);

XmlElement rootNode2 = xmlDoc.CreateElement("TrackRequest");
xmlDoc.AppendChild(rootNode2);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清风不识月 2024-08-02 18:25:25

一份 XML 文档只能有一个根节点。 否则它的形式就不好。 如果您需要同时发送两个 xml 文档,则需要创建 2 个 xml 文档并将它们连接在一起。

An XML document can only ever have one root node. Otherwise it's not well formed. You will need to create 2 xml documents and join them together if you need to send both at once.

表情可笑 2024-08-02 18:25:25

它抛出异常,因为您试图创建无效的 xml。 XmlDocument 只会生成格式良好的 xml。

您可以使用 XMLWriter 并将 XmlWriterSettings.ConformanceLevel 设置为 Fragment 来完成此操作,或者您可以创建两个 XmlDocuments 并将它们写入同一个流中。

Its throwing an exception because you are trying to create invalid xml. XmlDocument will only generate well formed xml.

You could do it using an XMLWriter and setting XmlWriterSettings.ConformanceLevel to Fragment or you could create two XmlDocuments and write them out into the same stream.

春夜浅 2024-08-02 18:25:25

构建两个单独的 XML 文档并连接它们的字符串表示形式。

Build two separate XML documents and concatenate their string representation.

故人如初 2024-08-02 18:25:25

看起来你的节点结构总是相同的。 (我没有看到任何条件逻辑。)如果结构是不变的,您可以定义一个 XML 模板字符串。 将该字符串加载到 XML 文档中并 执行 SelectNode 来填充各个节点。

这可能比以编程方式创建根、元素和元素更简单/更干净。 节点。

It looks like your node structure always be the same. (I don't see any conditional logic.) If the structure is constant you could define an XML template string. Load that string into an XML Document & do a SelectNode to populate individual nodes.

That may be simpler/cleaner than programatically creating the root, elements & nodes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文