简体   繁体   中英

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:

<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) . 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. How to approach this issue. Ps: I have not declared any namespace in my C# code.

This is the part of XML I'm trying to append:

<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.

This is the C# code written to create these nodes:

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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