我如何使用 XNamespace 将 2 个名称空间添加到 xml documento

How I add 2 namespaces to a xml documento with XNamespace

我正在尝试创建一个具有 2 个名称空间的 xml,一个没有任何前缀,另一个有前缀。我的问题是将两者都作为属性。

我尝试了很多解决方案,有些给出了错误的输出,有些给出了错误代码,例如 "System.Xml.XmlException: 'The prefix '' cannot be redefined from "

XNamespace ns1 = "http://www.issnetonline.com.br/webserviceabrasf/vsd/servico_enviar_lote_rps_envio.xsd", tc = "http://www.issnetonline.com.br/webserviceabrasf/vsd/tipos_complexos.xsd";
            el.Xmlns = "http://www.issnetonline.com.br/webserviceabrasf/vsd/servico_enviar_lote_rps_envio.xsd";

XDocument xdoc = new XDocument(
                new XDeclaration("1.0", "utf-8",""),
                new XElement(ns1+"EnviarLoteRpsEnvio",
                  //  new XAttribute("xmlns" ,ns1), <- HERE IS THE ISSUE
                    new XAttribute(XNamespace.Xmlns + "tc", "http://www.issnetonline.com.br/webserviceabrasf/vsd/tipos_complexos.xsd"),
                       new XElement("LoteRps",
                new XElement(tc + "NumeroLote",loteRps.NumeroLote),
                   new XElement(tc + "CpfCnpj",
                        new XElement(tc + "Cnpj", cnpj.Cnpj)
                                )
                        , new XElement(tc + "InscricaoMunicipal", loteRps.InscricaoMunicipal)
                        , new XElement(tc + "QuantidadeRps", loteRps.QuantidadeRps)
                        , ListaRps
                        )));



            string loteenvio = "-env-loterps.xml";
            string filename = cnpj.Cnpj + loteRps.NumeroLote + loteenvio;

            xdoc.Save(filename);

我的实际结果是:

<?xml version="1.0" encoding="utf-8"?>
<EnviarLoteRpsEnvio xmlns:tc="http://www.issnetonline.com.br/webserviceabrasf/vsd/tipos_complexos.xsd" xmlns="http://www.issnetonline.com.br/webserviceabrasf/vsd/servico_enviar_lote_rps_envio.xsd">
  <LoteRps xmlns="">
    <tc:NumeroLote>1</tc:NumeroLote>
    <tc:CpfCnpj>
      <tc:Cnpj>123456789</tc:Cnpj>
    </tc:CpfCnpj>

当我需要的时候是:

<?xml version="1.0" encoding="utf-8"?>
<EnviarLoteRpsEnvio xmlns="http://www.issnetonline.com.br/webserviceabrasf/vsd/servico_enviar_lote_rps_envio.xsd" xmlns:tc="http://www.issnetonline.com.br/webserviceabrasf/vsd/tipos_complexos.xsd">
  <LoteRps>
    <tc:NumeroLote>1</tc:NumeroLote>
    <tc:CpfCnpj>
      <tc:Cnpj>123456789</tc:Cnpj>
    </tc:CpfCnpj>

已找到解决方案! 我参考了Microsoft Docs,最后用这段代码解决了:

 XDocument xdoc = new XDocument(
                new XDeclaration("1.0", "utf-8",""),
                new XElement(ns1+"EnviarLoteRpsEnvio"
                    ,new XAttribute ("xmlns","http://www.issnetonline.com.br/webserviceabrasf/vsd/servico_enviar_lote_rps_envio.xsd")
                    ,new XAttribute(XNamespace.Xmlns + "tc", "http://www.issnetonline.com.br/webserviceabrasf/vsd/tipos_complexos.xsd")
                       ,new XElement(ns1+"LoteRps",
                new XElement(tc + "NumeroLote",loteRps.NumeroLote),

我最大的收获是子元素之前的 "ns1+",结果是 XML:

<?xml version="1.0" encoding="utf-8"?>
<EnviarLoteRpsEnvio xmlns="http://www.issnetonline.com.br/webserviceabrasf/vsd/servico_enviar_lote_rps_envio.xsd" xmlns:tc="http://www.issnetonline.com.br/webserviceabrasf/vsd/tipos_complexos.xsd">
  <LoteRps>
    <tc:NumeroLote>1</tc:NumeroLote>
    <tc:CpfCnpj>