简体   繁体   English

如何在 C# 中的 XMLNode 上插入新元素

[英]How to insert the new element on XMLNode in C#

I am trying to insert a new element into the XmlNode in C# code,我正在尝试在 C# 代码中向 XmlNode 中插入一个新元素,

How to insert the <delimiter>@#<delimiter> element in inside the "/TestBooks/template/field" root.如何在“/TestBooks/template/field”根目录中插入<delimiter>@#<delimiter>元素。 (Screenshot1) enter image description here (截图1)在此输入图片说明

Inside the <field> element, i need to insert the <delimiter> element, based on id element <Id>11-09-2020-505</Id> .<field>元素内,我需要根据 id 元素<Id>11-09-2020-505</Id>插入<delimiter>元素。 (Screenshot2) enter image description here (截图2)在此输入图片说明

First of all you really shouldn't use pictures in questions -- we are taking the time to type you an answer you can take the time to use copy and paste and format your questions.首先,您真的不应该在问题中使用图片——我们正在花时间为您输入答案,您可以花时间使用复制和粘贴以及格式化您的问题。

I'd like to answer your questions but I'm concerned about the questions because it implies that you are adding in markers to aid in the parsing of the data.我想回答您的问题,但我很担心这些问题,因为这意味着您正在添加标记以帮助解析数据。 You really shouldn't need to parse XML.您真的不需要解析 XML。

There are many great parsers of XML including one built into C# you should not roll your own.有许多出色的 XML 解析器,其中包括一个内置于 C# 中的解析器,您不应该使用自己的解析器。

In the XML standard the order of the children not defined.在 XML 标准中未定义子级的顺序。 If you put a child as the "first" child, there is no reason to expect that a parser would list it as first.如果你把一个孩子作为“第一个”孩子,就没有理由期望解析器会把它列为第一个。

(To have order in children you should just add an order attribute) (要在孩子中订购,您应该只添加一个 order 属性)

Because of these reason it should not matter where in the list of children you add the child.由于这些原因,您在孩子列表中的哪个位置添加孩子应该无关紧要。

So we can tell you how to add a child but we can't "put it in a specific spot" since children don't have an order.所以我们可以告诉你如何添加一个孩子,但我们不能“把它放在一个特定的地方”,因为孩子们没有订单。

In summary, it is not possible to do what you ask.总之,不可能按照你的要求去做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM