Reputation: 6042
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?
Upvotes: 2
Views: 3140
Reputation: 2761
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");
Upvotes: 3
Reputation: 2761
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>
Upvotes: 0