none
How to remove xml namespace prefixes in c# RRS feed

  • Question

  • Hello All, 

    I am working on xml document manipulation. Could anyone please help me to remove all prefixes in xml document root namespace using c#

    this is my input 

    <?xml version="1.0" encoding="UTF-8"?>
    <Facility xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://docs.buildingsmartalliance.org/nbims03/cobie/core" 
    core:externalID="3eM8WbY_59RR5TDWry5aRU" 

    xmlns:core="http://docs.buildingsmartalliance.org/nbims03/cobie/core" 
    xmlns:cobielite="http://docs.buildingsmartalliance.org/nbims03/cobie/cobielite" 
    core:externalEntityName="IfcBuilding"
    core:externalSystemName="Autodesk Revit Architecture 2011"
    xsi:schemaLocation="http://docs.buildingsmartalliance.org/nbims03/cobie/cobielite cobielite.xsd">
    <FacilityName>PN 0001</FacilityName>
    <FacilityCategory>11-13 24 14: Clinic</FacilityCategory>
    <ProjectAssignment core:externalEntityName="IfcProject" core:externalID="3eM8WbY_59RR5TDWry5aRV" core:externalSystemName="Autodesk Revit Architecture 2011">
    <ProjectName>Medical-Dental Clinic</ProjectName>
    <ProjectDescription>Medical-Dental Clinic</ProjectDescription>
    </ProjectAssignment>
    <SiteAssignment core:externalEntityName="IfcSite" core:externalID="3eM8WbY_59RR5TDWry5aRT" core:externalSystemName="Autodesk Revit Architecture 2011">
    <SiteName>PN 0001</SiteName>
    </SiteAssignment>
    <FacilityDefaultLinearUnit>meters</FacilityDefaultLinearUnit>
    <FacilityDefaultAreaUnit>square meters</FacilityDefaultAreaUnit>
    <FacilityDefaultVolumeUnit>cubic meters</FacilityDefaultVolumeUnit>
    <FacilityDefaultCurrencyUnit>Dollars</FacilityDefaultCurrencyUnit>
    <FacilityDefaultMeasurementStandard>Autodesk Revit Architecture 2011 BIM Area</FacilityDefaultMeasurementStandard>
    <FacilityDescription>Medical-Dental Clinic</FacilityDescription>
    <FacilityDeliverablePhaseName>Handover</FacilityDeliverablePhaseName>


    </Facility>

    i need below output

    <?xml version="1.0" encoding="UTF-8"?>
    <Facility xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://docs.buildingsmartalliance.org/nbims03/cobie/cobielite"
    externalID="3eM8WbY_59RR5TDWry5aRU"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <FacilityName>PN 0001</FacilityName>
    <FacilityCategory>11-13 24 14: Clinic</FacilityCategory>
    <ProjectAssignment externalID="3eM8WbY_59RR5TDWry5aRV" >
    <ProjectName>Medical-Dental Clinic</ProjectName>
    <ProjectDescription>Medical-Dental Clinic</ProjectDescription>
    </ProjectAssignment>
    <SiteAssignment externalID="3eM8WbY_59RR5TDWry5aRT" >
    <SiteName>PN 0001</SiteName>
    </SiteAssignment>
    <FacilityDefaultLinearUnit>meters</FacilityDefaultLinearUnit>
    <FacilityDefaultAreaUnit>square meters</FacilityDefaultAreaUnit>
    <FacilityDefaultVolumeUnit>cubic meters</FacilityDefaultVolumeUnit>
    <FacilityDefaultCurrencyUnit>Dollars</FacilityDefaultCurrencyUnit>
    <FacilityDefaultMeasurementStandard>Autodesk Revit Architecture 2011 BIM Area</FacilityDefaultMeasurementStandard>
    <FacilityDescription>Medical-Dental Clinic</FacilityDescription>
    <FacilityDeliverablePhaseName>Handover</FacilityDeliverablePhaseName>

    </Facility>

    Any one pls help me to achieve this using c#

    Thanks in Advance

    Chandu

    Wednesday, October 11, 2017 1:11 PM

Answers

  • Follow up code to remove the prefixes from the rest of the XML.

    static void RemoveNamespacePrefix ( XElement element )
    {
        //Remove from element
        if (element.Name.Namespace != null)
            element.Name = element.Name.LocalName;
    
        //Remove from attributes
        var attributes = element.Attributes().ToArray();
        element.RemoveAttributes();
        foreach (var attr in attributes)
        {
            var newAttr = attr;
    
            if (attr.Name.Namespace != null)
                newAttr = new XAttribute(attr.Name.LocalName, attr.Value);
    
            element.Add(newAttr);                    
        };
    
        //Remove from children
        foreach (var child in element.Descendants())
            RemoveNamespacePrefix(child);
    }
    
    //Usage
    var doc = XDocument.Load("");
    ...
    RemoveNamespacePrefix(doc.Root);
    


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, October 11, 2017 5:35 PM
    Moderator

All replies

  • Namespaces in XML documents are controlled via the XmlNamespaceManager and NameTable classes. In theory you just remove the namespaces from the document. But you'll also have to remove the prefixes from any impacted objects. I also notice that you only want to remove some of the namespaces so that complicates things. Here's how to remove the namespaces themselves (from code posted here).

    var doc = XDocument.Load("Test.xml");
    
    var namespaces = from a in doc.Descendants().Attributes()
                        where a.IsNamespaceDeclaration && a.Name != "xsi"
                        select a;
    namespaces.Remove();
    
    doc.Save("Test_New.xml");

    You just need to enumerate the elements/attributes and remove the prefix.


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, October 11, 2017 3:01 PM
    Moderator
  • Follow up code to remove the prefixes from the rest of the XML.

    static void RemoveNamespacePrefix ( XElement element )
    {
        //Remove from element
        if (element.Name.Namespace != null)
            element.Name = element.Name.LocalName;
    
        //Remove from attributes
        var attributes = element.Attributes().ToArray();
        element.RemoveAttributes();
        foreach (var attr in attributes)
        {
            var newAttr = attr;
    
            if (attr.Name.Namespace != null)
                newAttr = new XAttribute(attr.Name.LocalName, attr.Value);
    
            element.Add(newAttr);                    
        };
    
        //Remove from children
        foreach (var child in element.Descendants())
            RemoveNamespacePrefix(child);
    }
    
    //Usage
    var doc = XDocument.Load("");
    ...
    RemoveNamespacePrefix(doc.Root);
    


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, October 11, 2017 5:35 PM
    Moderator
  • Hi Michael thanks for the response. By above linq query i am able to remove name spaces. getting below response. 

    <?xml version="1.0" encoding="utf-8"?>
    <Facility p1:externalID="3eM8WbY_59RR5TDWry5aRU" p1:externalEntityName="IfcBuilding" p1:externalSystemName="Autodesk Revit Architecture 2011" p2:schemaLocation="http://docs.buildingsmartalliance.org/nbims03/cobie/cobielite cobielite.xsd" xmlns:p2="http://www.w3.org/2001/XMLSchema-instance" xmlns:p1="http://docs.buildingsmartalliance.org/nbims03/cobie/core" xmlns="http://docs.buildingsmartalliance.org/nbims03/cobie/core">
      <p1:FacilityName>PN 0001</p1:FacilityName>
      <p1:FacilityCategory>11-13 24 14: Clinic</p1:FacilityCategory>
      <p1:ProjectAssignment p1:externalEntityName="IfcProject" p1:externalID="3eM8WbY_59RR5TDWry5aRV" p1:externalSystemName="Autodesk Revit Architecture 2011">
        <p1:ProjectName>Medical-Dental Clinic</p1:ProjectName>
        <p1:ProjectDescription>Medical-Dental Clinic</p1:ProjectDescription>
      </p1:ProjectAssignment>
      <p1:SiteAssignment p1:externalEntityName="IfcSite" p1:externalID="3eM8WbY_59RR5TDWry5aRT" p1:externalSystemName="Autodesk Revit Architecture 2011">
        <p1:SiteName>PN 0001</p1:SiteName>
      </p1:SiteAssignment>
      <p1:FacilityDefaultLinearUnit>meters</p1:FacilityDefaultLinearUnit>
      <p1:FacilityDefaultAreaUnit>square meters</p1:FacilityDefaultAreaUnit>
      <p1:FacilityDefaultVolumeUnit>cubic meters</p1:FacilityDefaultVolumeUnit>
      <p1:FacilityDefaultCurrencyUnit>Dollars</p1:FacilityDefaultCurrencyUnit>
      <p1:FacilityDefaultMeasurementStandard>Autodesk Revit Architecture 2011 BIM Area</p1:FacilityDefaultMeasurementStandard>
      <p1:FacilityDescription>Medical-Dental Clinic</p1:FacilityDescription>
      <p1:FacilityDeliverablePhaseName>Handover</p1:FacilityDeliverablePhaseName>
    </Facility>

    Here i need to remove child elements name spaces prefixes p1, p2 also. i tried all scenarios to remove these prefixes. but didn't get fruitful result. If any possibility to achieve, please help me. 

    Thanks

    Chandu

    Wednesday, October 11, 2017 5:57 PM
  • That's what the second code block I posted does.

    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, October 11, 2017 7:24 PM
    Moderator
  • Thanks a lot Michael. U maky my day beautiful. :) :)
    Thursday, October 12, 2017 4:59 PM