2

I have a string containing a partial XML fragment, which may contain various undeclared namespaces and therefore cannot be parsed by the XML parser I'm using (.Net's XElement.Parse):

<elements>
    <removeThis:element attribute="value">
        Contents
    </removeThis:element>
</elements>

So before passing the string to the XML parser I need to strip the namespaces from the string (I don't need the namespaces, I just need the fragment to parse):

<elements>
    <element attribute="value">
        Contents
    </element>
</elements>

Any suggestions on ways to achieve this result, e.g. a Regular Expression, or some option I'm not ware of within .Net's XML parser?

2
  • Is this something that you are looking for ? stackoverflow.com/questions/24305747/… Commented May 12, 2015 at 12:13
  • No sadly that answer does not help with removing the namespace prefix from the elements Commented May 12, 2015 at 12:30

2 Answers 2

3

Method with regular expressions. This workes if xml did not contain CData and replaces only element names (not attributes).

// read xml string
string input = File.ReadAllText(@"D:\Temp\text.txt");

// replace
string output = Regex.Replace(input, @"(<\s*\/?)\s*(\w+):(\w+)", "$1$3");
Sign up to request clarification or add additional context in comments.

1 Comment

Nice, thank you, seems to work well after a quick initial test
0

Sample xml:

<elements xmlns:removeThis="xmlnsname">
    <removeThis:element attribute="value">
        Contents
    </removeThis:element>
</elements>

Code:

private static void RemoveNamespaces(XElement element)
{
    // remove namespace prefix
    element.Name = element.Name.LocalName;

    // remove namespaces from children elements
    foreach (var elem in element.Elements())
    {
        RemoveNamespaces(elem);
    }

    // remove namespace attributes
    foreach (var attr in element.Attributes())
    {
        if (attr.IsNamespaceDeclaration)
        {
            attr.Remove();
        }
    }
}

Usage (I save sample xml in file 'D:\Temp\temp.txt'):

var elem = XElement.Parse(File.ReadAllText(@"D:\Temp\text.txt"));
RemoveNamespaces(elem);
using (var writer = XmlWriter.Create(@"D:\Temp\text.txt", new XmlWriterSettings
    {
        Indent = true,
        OmitXmlDeclaration = true,
    }))
{
    elem.WriteTo(writer);
}

Result:

<elements>
  <element attribute="value">
        Contents
    </element>
</elements>

1 Comment

Thanks for the answer, but the input is a string that cannot be parsed using XElement.Parse(string) as the missing namespace declarations cause this error: "Could not parse input to valid XDocument"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.