我正在尝试创建一个带有2个命名空间的xml,一个没有任何前缀,另一个带有前缀。我把它们作为属性的问题。
我尝试了很多解决方案,有些输出错误,其他错误代码,如“System.Xml.XmlException:'前缀''无法重新定义”
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>