C# Linq to XML – Keeping namespaces in parent nodes while removing them from child nodes

So I have this XML tree created through LINQ to XML, like this:

   XNamespace xNamespace = "urn:OECD:StandardAuditFile-Tax:PT_";
   XElement main =
    new XElement(xNamespace + "AuditFile",
    new XAttribute("xmlns", "urn:OECD:StandardAuditFile-Tax:PT_"),
    new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
    new XElement(xNamespace + "Header",
          new XAttribute("xmlns", "urn:OECD:StandardAuditFile-Tax:PT_"),
          new XElement("AuditFileVersion", "filler"),
          new XElement("CompanyID", "filler"),
          new XElement("TaxRegistrationNumber", "filler"),
          new XElement("TaxAccountingBasis", "filler"),
          new XElement("CompanyName", "filler"),
          new XElement("BusinessName", "filler"),
          new XElement("CompanyAddress",
             new XElement("AddressDetail", "filler"),
             new XElement("City", "filler"),
             new XElement("PostalCode", "filler"),
             new XElement("Region", "filler"),
             new XElement("Country", "filler")
         ),
         new XElement("FiscalYear", "filler"),
         new XElement("StartDate", "filler"),
         new XElement("EndDate", "filler"),
         new XElement("CurrencyCode", "filler"),
         new XElement("DateCreated", "filler"),
         new XElement("TaxEntity", "filler"),
         new XElement("ProductCompanyTaxID", "filler"),
         new XElement("SoftwareCertificateNumber", "filler"),
         new XElement("ProductID", "filler"),
         new XElement("ProductVersion", "filler"),
         new XElement("Telephone", "filler"),
         new XElement("Fax", "filler"),
         new XElement("Email", "filler")
     ));

The contents are just filler since they originally display sensitive information that doesn’t really matter for this topic. As you can see, I added the namespaces to the root node AuditFile and to node Header. The xml files comes out like this:

<AuditFile xmlns="urn:OECD:StandardAuditFile-Tax:PT_1_04_01" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <Header xmlns="urn:OECD:StandardAuditFile-Tax:PT_1_04_01">
     <AuditFileVersion xmlns="">filler</AuditFileVersion>
     <CompanyID xmlns="">filler</CompanyID>
     <TaxRegistrationNumber xmlns="">filler</TaxRegistrationNumber>
     <TaxAccountingBasis xmlns="">filler</TaxAccountingBasis>
     <CompanyName xmlns="">filler</CompanyName>
     <BusinessName xmlns="">filler</BusinessName>
     <CompanyAddress xmlns="">
         <AddressDetail>filler</AddressDetail>
         <City>filler</City>
         <PostalCode>filler</PostalCode>
         <Region>filler</Region>
        <Country>filler</Country>
     </CompanyAddress>
     <FiscalYear xmlns="">filler</FiscalYear>
     <StartDate xmlns="">filler</StartDate>
     <EndDate xmlns="">filler</EndDate>
     <CurrencyCode xmlns="">filler</CurrencyCode>
     <DateCreated xmlns="">filler</DateCreated>
     <TaxEntity xmlns="">filler</TaxEntity>
     <ProductCompanyTaxID xmlns="">filler</ProductCompanyTaxID>
     <SoftwareCertificateNumber xmlns="">filler</SoftwareCertificateNumber>
     <ProductID xmlns="">filler</ProductID>
     <ProductVersion xmlns="">filler</ProductVersion>
     <Telephone xmlns="">filler</Telephone>
     <Fax xmlns="">filler</Fax>
     <Email xmlns="">filler</Email>
  </Header>
</AuditFile>

All the other nodes also have the xmlns label, I wanted to remove that while also keeping the namespaces for the AuditFile and Header nodes, is that possible?

1

Basically it sounds like you want all the elements to be in the same namespace – which is inherited from parents via xmlns. So you need to specify the namespace for all the elements, e.g.

XNamespace xNamespace = "urn:OECD:StandardAuditFile-Tax:PT_";
XElement main =
    new XElement(xNamespace + "AuditFile",
    new XAttribute("xmlns", "urn:OECD:StandardAuditFile-Tax:PT_"),
    new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
    new XElement(xNamespace + "Header",
          new XAttribute("xmlns", "urn:OECD:StandardAuditFile-Tax:PT_"),
          new XElement(xNamespace + "AuditFileVersion", "filler"),
          new XElement(xNamespace + "CompanyID", "filler"),
          new XElement(xNamespace + "TaxRegistrationNumber", "filler"),
          new XElement(xNamespace +"TaxAccountingBasis", "filler"),
          new XElement(xNamespace +"CompanyName", "filler"),
          ...

That way, you won’t end up with xmlns="" in AuditFileVersion etc, because it’ll be in the “right” namespace based on the parent element.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật