Setting the namespace during parsing

Hey. I am currently having a problem parsing an Xml string without any namespace and added to an existing XElement with a namespace.

my code is:

XElement elem = root.Element(xs + "methodCall");
if (elem != null)
{
    XElement e = XElement.Parse(this.MethodCallXML);

    elem.Add(e);
}

result:

<methodCall>
  <methodCall service="activity" method="activityDeleteComment" xmlns="">
    <espSessionState>espSessionState1</espSessionState>
    <traceFlowCode>true</traceFlowCode>
    <params>
      <commentID>http://uri1</commentID>
      <isPermanentDelete>false</isPermanentDelete>
    </params>
  </methodCall>
</methodCall>

My problem is that xmlns = "" I cannot figure out how to create a node using the parsing method and provide it with a default namespace.

Is there any way to do this?

+5
source share
1 answer

Ok, I figured out how to add a namespace to the new XElement and all descendants

foreach (XElement ce in e.DescendantsAndSelf())
     ce.Name = xs + ce.Name.LocalName;

So far this solves my problem, but if someone can see a potential flaw or an easier way to do this, please let me know.

+8
source

All Articles