I have an XMLdoc , the below is just a snippet
<APCS:ParameterList>
<APCS:ParameterGroup name="GROUP">
<APCS:ParameterGroup name="W1">
<APCS:Parameter name="BASE">1</APCS:Parameter>
<APCS:Parameter name="LOOP">4</APCS:Parameter>
<APCS:Parameter name="WIRE">-1</APCS:Parameter>
<APCS:Parameter name="THREAD">-1</APCS:Parameter>
</APCS:ParameterGroup>
</APCS:ParameterGroup>
</APCS:ParameterList>
I need to remove the APCS namespace and return the doc as below
<ParameterList>
<ParameterGroup name="GROUP">
<ParameterGroup name="W1">
<Parameter name="BASE">1</Parameter>
<Parameter name="LOOP">4</Parameter>
<Parameter name="WIRE">-1</Parameter>
<Parameter name="THREAD">-1</Parameter>
</ParameterGroup>
</ParameterGroup>
</ParameterList>
What I have tried:
I have helper methods that can remove the namespace, however this is also removing the number values i.e.
<Parameter name="BASE" />
The methods are below.
private static XmlElement changeNameSpaceRecursionHelper(XmlDocument doc, XmlElement fragment, string originalNamespace, string targetNamespace)
{
string newElementNamespace = (fragment.NamespaceURI == originalNamespace ? targetNamespace : fragment.NamespaceURI);
XmlElement newElement = doc.CreateElement(fragment.LocalName, newElementNamespace);
foreach (XmlNode node in fragment.ChildNodes)
{
if (node is XmlElement)
{
newElement.AppendChild(changeNameSpaceRecursionHelper(doc, (XmlElement)node, originalNamespace, targetNamespace));
}
}
foreach (XmlAttribute attr in fragment.Attributes)
{
string newAttributeNamespace = (attr.NamespaceURI == originalNamespace ? targetNamespace : attr.NamespaceURI);
XmlAttribute newAttribute = doc.CreateAttribute(attr.LocalName, newAttributeNamespace);
newAttribute.Value = attr.Value;
newElement.Attributes.Append(newAttribute);
}
return newElement;
}
public static XmlElement ChangeNameSpace(XmlElement fragment, string originalNamespace, string targetNamespace)
{
XmlDocument doc = new XmlDocument();
return changeNameSpaceRecursionHelper(doc, fragment, originalNamespace, targetNamespace);
}