如何从Xml文档中删除命名空间

时间:2012-05-03 19:42:19

标签: c# xml xml-parsing

我在从文档中删除命名空间时遇到一些麻烦,以便专有的Parse()函数正常工作。我收到的XML字符串如下所示:

<r xmlns:p="p" xmlns:s="s" p:p="1">
<d id="{FE5D7FDF-89C0-4D99-9AA3-B5FBD009C9F3}">
    <r uid="{F72A4E1A-519B-42DA-A1E8-F7D10EA92C65}" p:before="r[@uid='{15E0854F-39D8-4890-8541-9C4102C0F944}']" s:id="{6AF25CA3-6AB7-4792-B582-5DE0DF6349D7}" s:ph="/body/main/tab_content/body_content_column"/>
    <r uid="{15E0854F-39D8-4890-8541-9C4102C0F944}" p:before="r[@uid='{7CCB5501-52DF-49F6-BE71-E7B0CA68EE3B}']" s:id="{FD61546D-1226-4292-85F2-656883507F45}" s:ph="/body/main/tab_content/body_content_column"/>
    <r uid="{7CCB5501-52DF-49F6-BE71-E7B0CA68EE3B}" p:before="r[@uid='{17AC1E20-55CB-4EA1-9073-35D9E776C9A3}']" s:id="{1778C38C-071A-4219-A141-4686B095E361}" s:ph="/body/main/tab_content"/>
    <r uid="{17AC1E20-55CB-4EA1-9073-35D9E776C9A3}" p:before="r[@uid='{9B34F931-3C72-40D2-90B7-D4DCC81163D5}']" s:id="{C1CF5E12-1793-4F2C-B49C-00D30F0F501F}" s:ph="/body/hero/booking_widget"/>
    <r uid="{9B34F931-3C72-40D2-90B7-D4DCC81163D5}" p:before="r[@uid='{957B0932-A2B0-406F-A2D4-8C37E9F9D3A7}']" s:cac="0" s:cnd="" s:mvt="" s:ds="" s:id="{DF8E2887-0BCA-4C46-B2E4-02CDD3652892}" s:par="" s:ph="/body/main/tab_content/body_tile_column" s:vbd="0" s:vbdev="0" s:vbl="0" s:vbp="0" s:vbqs="0" s:vbu="0"/>
    <r uid="{957B0932-A2B0-406F-A2D4-8C37E9F9D3A7}" p:before="r[@uid='{B6EBC78C-5C85-4256-9F9D-3DF2F907DFA6}']" s:cac="0" s:cnd="" s:mvt="" s:ds="" s:id="{69FF80E2-6E79-4556-AC95-E409121B15C7}" s:par="MapSize=mini" s:ph="/body/main/tab_content/body_tile_column" s:vbd="0" s:vbdev="0" s:vbl="0" s:vbp="0" s:vbqs="0" s:vbu="0"/>
    <r uid="{B6EBC78C-5C85-4256-9F9D-3DF2F907DFA6}" p:before="r[@uid='{6700A64F-D8B7-44D3-8BE8-A162563ADB01}']" s:cac="0" s:cnd="" s:mvt="" s:ds="" s:id="{A6F5FC25-AB71-4C3B-B7FA-EF1F790DA86A}" s:par="" s:ph="/body/main/tab_content/body_tile_column" s:vbd="0" s:vbdev="0" s:vbl="0" s:vbp="0" s:vbqs="0" s:vbu="0"/>
    <r uid="{6700A64F-D8B7-44D3-8BE8-A162563ADB01}" p:before="r[@uid='{9863B72F-AE05-4D34-B271-E1AD6D67B946}']" s:id="{0430BDF5-8E53-4B12-A8A2-C098F667E575}" s:ph="/body/main/tab_content/print_share_toolbar"/>
    <r uid="{9863B72F-AE05-4D34-B271-E1AD6D67B946}" p:before="r[@uid='{166944E6-FCEA-4CE6-A5DC-EC317337046B}']" s:id="{FDF0D36C-1863-4DAA-BB68-555A2F3A270A}" s:ph="/body/main/tab_menu"/>
    <r uid="{166944E6-FCEA-4CE6-A5DC-EC317337046B}" p:before="r[@uid='{E98B1E29-46AF-46A3-89C0-441443E97BA0}']" s:id="{F94A415B-6922-43F2-9DEE-F012DE1497BA}" s:ph="/body/main"/>
    <r uid="{E98B1E29-46AF-46A3-89C0-441443E97BA0}" p:before="r[@uid='{B03EF589-3103-448A-8105-BFC33BA953D0}']" s:id="{EAFA90C7-136A-41C8-9AE9-8B1123E7E3C9}" s:ph="body"/>
    <r uid="{969938EE-BF4F-4A42-BA30-A7AE12183B08}">
        <p:d/>
    </r>
    <r uid="{AAA093DC-A63F-4538-A94B-ECBB509008F5}">
        <p:d/>
    </r>
    <r uid="{16A7A1FE-5FB4-4394-B053-58147F3B584D}">
        <p:d/>
    </r>
    <r uid="{4CD0B2F1-CAC8-4C03-B11E-56AE8A2589E1}" p:before="*[1=2]"/>
</d>
</r>

我正在使用编辑实用程序以HTML格式查看上述XML,该输出显示相同的输出:

<r xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<d id="{FE5D7FDF-89C0-4D99-9AA3-B5FBD009C9F3}" l="{EAB1DFDD-9918-4158-862B-BC3FD02B00CC}">
    <r id="{FC207214-4E05-40E9-B3C5-49AE41B6A323}" ph="head" uid="{B03EF589-3103-448A-8105-BFC33BA953D0}"/>
    <r id="{EAFA90C7-136A-41C8-9AE9-8B1123E7E3C9}" ph="body" uid="{969938EE-BF4F-4A42-BA30-A7AE12183B08}"/>
    <r id="{9325E0F7-A4B5-46EC-B178-E7EF051FB1FE}" ph="hero" uid="{6C998C3F-C968-466A-AB3A-C9C12C78A389}"/>
    <r id="{7728075C-15DE-4DBE-9018-32794A1540F0}" ph="breadcrumb" uid="{593B65AF-BFA4-45F0-9EF0-12806CF54468}"/>
    <r id="{C1CF5E12-1793-4F2C-B49C-00D30F0F501F}" ph="booking_widget" uid="{AAA093DC-A63F-4538-A94B-ECBB509008F5}"/>
    <r id="{2BA579A5-06C8-41D7-B170-EA745D4CDE8E}" ph="image_rotator" uid="{CC96DC9A-60EB-4C68-8F32-06838EB69818}"/>
    <r uid="{F8E91E5B-1C55-42F6-96C2-52B1FE86ECEA}" id="{C8B42985-3D99-4E08-951C-2C3A98A737B4}" ph="main"/>
    <r uid="{209FDA28-CA6D-4306-9587-294E35849620}" id="{0430BDF5-8E53-4B12-A8A2-C098F667E575}" ph="print_share_toolbar"/>
    <r id="{F94A415B-6922-43F2-9DEE-F012DE1497BA}" ph="main" uid="{B14A545D-F2F1-4AAB-8463-8034ABD06826}"/>
    <r id="{FDF0D36C-1863-4DAA-BB68-555A2F3A270A}" ph="tab_menu" uid="{16A7A1FE-5FB4-4394-B053-58147F3B584D}"/>
    <r id="{1778C38C-071A-4219-A141-4686B095E361}" ph="/body/main/tab_content" uid="{4CD0B2F1-CAC8-4C03-B11E-56AE8A2589E1}"/>
    <r id="{E863286C-76C6-4078-A718-09E0AF129D68}" ph="foot" uid="{D6163B3C-0B93-4ADE-8657-E6AB4DF84B1E}"/>
    <r uid="{26A873D4-2928-401D-A5AF-F71041A65AB9}" cac="0" cnd="" mvt="" ds="" id="{2834C1FA-1150-4784-B4DF-A743DB3EF0F4}" par="" ph="special_rates_sidebar" vbd="0" vbdev="0" vbl="0" vbp="0" vbqs="0" vbu="0"/>
</d>
<d id="{46D2F427-4CE5-4E1F-BA10-EF3636F43534}" l="{1B03BFE2-32C4-4C12-A4AE-C966CEB3F7FD}">
    <r id="{E7FB95F1-9E08-412E-A9DF-7F338C74D220}" ph="head" uid="{C409527B-CDC3-4D20-B9DD-27DE389904C7}"/>
    <r cac="0" cnd="" mvt="" ds="" id="{789ABFC6-2950-485D-B600-C139B7FEAA9B}" par="" ph="" uid="{E63EE34C-639D-493F-A3BC-DCF782EFC75B}" vbd="0" vbdev="0" vbl="0" vbp="0" vbqs="0" vbu="0"/>
    <r cac="0" cnd="" mvt="" ds="" id="{255C6F91-525C-4665-B897-1CF9282F59AA}" par="" ph="body" uid="{2373A2F1-3AD8-440E-B401-1169BF6BB8AA}" vbd="0" vbdev="0" vbl="0" vbp="0" vbqs="0" vbu="0"/>
</d>
</r>

这是我需要XML来引入Parse()函数来正确解析数据的格式。但是我似乎无法在代码中使用它。我尝试过此处描述的解决方案:How to remove namespace prefix. (C#)但是当我调用Remove()方法时,s:前缀会转换为类似p:7的内容。

有人可以就如何将我的第一个例子转换为第二个例子给我一些建议。

编辑:这是我现在的代码和我得到的输出

XDocument xDoc = XDocument.Parse(renderingProperty);
xDoc.Root.RemoveAttributes();
xDoc.Root.Add(new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance\\"));
xDoc.Root.Add(new XAttribute(XNamespace.Xmlns + "xsd", "http://www.w3.org/2001/XMLSchema\\"));

输出

<r xmlns:p="p" xmlns:s="s" p:p="1">
<d id="{FE5D7FDF-89C0-4D99-9AA3-B5FBD009C9F3}">
    <r uid="{F72A4E1A-519B-42DA-A1E8-F7D10EA92C65}" p:before="r[@uid='{15E0854F-39D8-4890-8541-9C4102C0F944}']" s:id="{6AF25CA3-6AB7-4792-B582-5DE0DF6349D7}" s:ph="/body/main/tab_content/body_content_column"/>
    <r uid="{15E0854F-39D8-4890-8541-9C4102C0F944}" p:before="r[@uid='{7CCB5501-52DF-49F6-BE71-E7B0CA68EE3B}']" s:id="{FD61546D-1226-4292-85F2-656883507F45}" s:ph="/body/main/tab_content/body_content_column"/>
    <r uid="{7CCB5501-52DF-49F6-BE71-E7B0CA68EE3B}" p:before="r[@uid='{17AC1E20-55CB-4EA1-9073-35D9E776C9A3}']" s:id="{1778C38C-071A-4219-A141-4686B095E361}" s:ph="/body/main/tab_content"/>
    <r uid="{17AC1E20-55CB-4EA1-9073-35D9E776C9A3}" p:before="r[@uid='{9B34F931-3C72-40D2-90B7-D4DCC81163D5}']" s:id="{C1CF5E12-1793-4F2C-B49C-00D30F0F501F}" s:ph="/body/hero/booking_widget"/>
    <r uid="{9B34F931-3C72-40D2-90B7-D4DCC81163D5}" p:before="r[@uid='{957B0932-A2B0-406F-A2D4-8C37E9F9D3A7}']" s:cac="0" s:cnd="" s:mvt="" s:ds="" s:id="{DF8E2887-0BCA-4C46-B2E4-02CDD3652892}" s:par="" s:ph="/body/main/tab_content/body_tile_column" s:vbd="0" s:vbdev="0" s:vbl="0" s:vbp="0" s:vbqs="0" s:vbu="0"/>
    <r uid="{957B0932-A2B0-406F-A2D4-8C37E9F9D3A7}" p:before="r[@uid='{B6EBC78C-5C85-4256-9F9D-3DF2F907DFA6}']" s:cac="0" s:cnd="" s:mvt="" s:ds="" s:id="{69FF80E2-6E79-4556-AC95-E409121B15C7}" s:par="MapSize=mini" s:ph="/body/main/tab_content/body_tile_column" s:vbd="0" s:vbdev="0" s:vbl="0" s:vbp="0" s:vbqs="0" s:vbu="0"/>
    <r uid="{B6EBC78C-5C85-4256-9F9D-3DF2F907DFA6}" p:before="r[@uid='{6700A64F-D8B7-44D3-8BE8-A162563ADB01}']" s:cac="0" s:cnd="" s:mvt="" s:ds="" s:id="{A6F5FC25-AB71-4C3B-B7FA-EF1F790DA86A}" s:par="" s:ph="/body/main/tab_content/body_tile_column" s:vbd="0" s:vbdev="0" s:vbl="0" s:vbp="0" s:vbqs="0" s:vbu="0"/>
    <r uid="{6700A64F-D8B7-44D3-8BE8-A162563ADB01}" p:before="r[@uid='{9863B72F-AE05-4D34-B271-E1AD6D67B946}']" s:id="{0430BDF5-8E53-4B12-A8A2-C098F667E575}" s:ph="/body/main/tab_content/print_share_toolbar"/>
    <r uid="{9863B72F-AE05-4D34-B271-E1AD6D67B946}" p:before="r[@uid='{166944E6-FCEA-4CE6-A5DC-EC317337046B}']" s:id="{FDF0D36C-1863-4DAA-BB68-555A2F3A270A}" s:ph="/body/main/tab_menu"/>
    <r uid="{166944E6-FCEA-4CE6-A5DC-EC317337046B}" p:before="r[@uid='{E98B1E29-46AF-46A3-89C0-441443E97BA0}']" s:id="{F94A415B-6922-43F2-9DEE-F012DE1497BA}" s:ph="/body/main"/>
    <r uid="{E98B1E29-46AF-46A3-89C0-441443E97BA0}" p:before="r[@uid='{B03EF589-3103-448A-8105-BFC33BA953D0}']" s:id="{EAFA90C7-136A-41C8-9AE9-8B1123E7E3C9}" s:ph="body"/>
    <r uid="{969938EE-BF4F-4A42-BA30-A7AE12183B08}">
        <p:d/>
    </r>
    <r uid="{AAA093DC-A63F-4538-A94B-ECBB509008F5}">
        <p:d/>
    </r>
    <r uid="{16A7A1FE-5FB4-4394-B053-58147F3B584D}">
        <p:d/>
    </r>
    <r uid="{4CD0B2F1-CAC8-4C03-B11E-56AE8A2589E1}" p:before="*[1=2]"/>
</d>
</r>

谢谢!

1 个答案:

答案 0 :(得分:5)

您的问题是您要从根元素中删除命名空间前缀,但它仍然用于在文档的其余部分中限定属性本身。您希望首先从整个文档中删除命名空间。

public static class XmlExtensions
{
    public static void StripNamespace(this XDocument document)
    {
        if (document.Root == null) return;

        foreach (var element in document.Root.DescendantsAndSelf())
        {
            element.Name = element.Name.LocalName;
            element.ReplaceAttributes(GetAttributesWithoutNamespace(element));
        }
    }

    static IEnumerable GetAttributesWithoutNamespace(XElement xElement)
    {
        return xElement.Attributes()
            .Where(x => !x.IsNamespaceDeclaration)
            .Select(x => new XAttribute(x.Name.LocalName, x.Value));
    }
}

然后简单地说:

XDocument xDoc = XDocument.Parse(xml);
xDoc.StripNamespace();
xDoc.Root.Add(new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance\\"));
xDoc.Root.Add(new XAttribute(XNamespace.Xmlns + "xsd", "http://www.w3.org/2001/XMLSchema\\"));

这会生成您所使用的XML。