简体   繁体   English

C#将节点追加到现有XML(包含)名称空间,但新节点获得空白名称空间

[英]C# Appending nodes to an existing XML (contains) namespace but new nodes get blank namespaces

I am having an XML which contains the following namespace: 我有一个包含以下名称空间的XML:

<AUTOSAR xsi:schemaLocation="http://autosar.org/3.1.4 autosar_3-1-4.xsd" xmlns="http://autosar.org/3.1.4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

I have written the code to append nodes a particular level in the XML file, everything is working fine but for the node that I 1st create, it adds, For ex: < FUNCTION-NAME-VALUE xmlns="" > (the part in the bold text) . 我已经编写了将节点追加到XML文件中特定级别的代码,一切正常,但是对于我第一次创建的节点,它添加了,例如:<FUNCTION-NAME-VALUE xmlns =“” >(粗体)。 I want this tag to have the namespace as all the other tags in the XML, but this is the extra part that is automatically generated when a new node is created. 我希望此标签具有XML中所有其他标签的名称空间,但这是在创建新节点时自动生成的多余部分。 How to approach this issue. 如何解决这个问题。 Ps: I have not declared any namespace in my C# code. 附:我尚未在C#代码中声明任何名称空间。

This is the part of XML I'm trying to append: 这是我要附加的XML的一部分:

<FUNCTION-NAME-VALUE>
    <DEFINITION-REF DEST="FUNCTION-NAME-DEF">/AUTOSAR/Com/ComConfig/ComSignal/ComInvalidNotification</DEFINITION-REF>
    <VALUE>Rte_COMCbkInv_EPS_SteeringTorque</VALUE>
</FUNCTION-NAME-VALUE>

This is the issue I'm facing: 这是我面临的问题:

<FUNCTION-NAME-VALUE xmlns="">
    <DEFINITION-REF DEST="FUNCTION-NAME-DEF">/AUTOSAR/Com/ComConfig/ComSignal/ComInvalidNotification</DEFINITION-REF>
    <VALUE>Rte_COMCbkInv_EPS_SteeringTorque</VALUE>
</FUNCTION-NAME-VALUE>

Of all these tags that I'm creating, only the 1st tag is getting that xmlns:"" issue. 在我创建的所有这些标签中,只有第一个标签正在获取该xmlns:“”问题。

This is the C# code written to create these nodes: 这是编写用于创建这些节点的C#代码:

if (funChilds[m].InnerText != "/AUTOSAR/Com/ComConfig/ComSignal/ComNotification")
{
    XmlNode newNode = doc.CreateElement("FUNCTION-NAME-VALUE");
    XmlNode defNode_func = doc.CreateElement("DEFINITION-REF");
    XmlAttribute attr = doc.CreateAttribute("DEST");
    attr.Value = "FUNCTION-NAME-DEF";
    defNode_func.Attributes.SetNamedItem(attr);
    defNode_func.InnerText = "/AUTOSAR/Com/ComConfig/ComSignal/ComInvalidNotification";
    XmlNode valNode_func = doc.CreateElement("VALUE");
    valNode_func.InnerText = ("");
    newNode.AppendChild(defNode_func);
    newNode.AppendChild(valNode_func);
    def_ref[j].AppendChild(newNode);
}

Please let me know the necessary changes that are to be made to get rid of this issue. 请让我知道为摆脱此问题而需要进行的必要更改。

Fixed! 固定!

If namespace is specified for the newly created element, the problem of blank namespaces can be resolved! 如果为新创建的元素指定了名称空间,则可以解决空白名称空间的问题!

 XmlNode defNode_func = doc.CreateElement("DEFINITION-REF", "NamespaceURI");

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

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