1

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?

TimS
  • 5,922
  • 6
  • 35
  • 55
  • Is this something that you are looking for ? http://stackoverflow.com/questions/24305747/how-to-remove-xmlns-attribute-of-a-node-other-than-root-in-an-xdocument – Ashutosh Vyas May 12 '15 at 12:13
  • No sadly that answer does not help with removing the namespace prefix from the elements – TimS May 12 '15 at 12:30

2 Answers2

1

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");
General-Doomer
  • 2,681
  • 13
  • 13
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>
General-Doomer
  • 2,681
  • 13
  • 13
  • 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" – TimS May 12 '15 at 12:24