代码之家  ›  专栏  ›  技术社区  ›  MuchoG

C#Linq到XML-将命名空间保留在父节点中,同时将其从子节点中删除

  •  0
  • MuchoG  · 技术社区  · 11 月前

    所以我通过LINQ to XML创建了这个XML树,如下所示:

       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")
         ));
    

    这些内容只是填充,因为它们最初显示的是对本主题并不重要的敏感信息。正如您所看到的,我将名称空间添加到根节点AuditFile和节点Header中。xml文件如下:

    <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>
    

    所有其他节点也有xmlns标签,我想删除它,同时保留AuditFile和Header节点的名称空间,这可能吗?

    1 回复  |  直到 11 月前
        1
  •  2
  •   Jon Skeet    11 月前

    基本上,这听起来像是你想要的 全部的 元素应位于同一命名空间中,该命名空间通过以下方式从父级继承 xmlns 因此,你需要 指定 所有元素的名称空间,例如。

    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"),
              ...
    

    这样,你就不会得到 xmlns="" 在里面 AuditFileVersion 等等,因为它将位于基于父元素的“正确”命名空间中。