简体   繁体   English

从XML字符串片段中删除元素名称空间前缀

[英]Remove element namespace prefixes from XML string fragment

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 ): 我有一个包含部分XML片段的字符串,该片段可能包含各种未声明的名称空间,因此无法由我使用的XML解析器(.Net的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): 因此,在将字符串传递给XML解析器之前,我需要从字符串中剥离名称空间(我不需要名称空间,我只需要片段即可解析):

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

Any suggestions on ways to achieve this result, eg a Regular Expression, or some option I'm not ware of within .Net's XML parser? 关于实现此结果的方法的任何建议,例如正则表达式,或者.Net XML解析器中我不知道的某些选项?

Method with regular expressions. 具有正则表达式的方法。 This workes if xml did not contain CData and replaces only element names (not attributes). 如果xml不包含CData且仅替换元素名称(而不是属性),则此方法有效。

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

// replace
string output = Regex.Replace(input, @"(<\s*\/?)\s*(\w+):(\w+)", "$1$3");

Sample xml: 样本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'): 用法(我将示例xml保存在文件'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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM