2016-11-17 110 views
0

我正嘗試使用XMLSerializer在少數元素中的一個元素中生成名稱空間前綴的XML。只需要在一個XML元素中添加XML名稱空間前綴

下面是代碼 -

var commRemision = new Contracts.Dtos.Remision(); 
Transformer transformedRequest = new Transformer(); 
commRemision = transformedRequest.TransformRequest(dgRemision); 

public const string ElementNamespace = "http://www.buzonfiscal.com/ns/xsd/bf/remision/52"; 

XmlSerializerNamespaces xmlNamespace = new XmlSerializerNamespaces(); 
xmlNamespace.Add(string.Empty, ElementNamespace); ///remove default namespaces 

var serializer = new XmlSerializer(commRemision.GetType(), ElementNamespace); 

XmlWriterSettings settings = new XmlWriterSettings(); 
settings.Encoding = new UnicodeEncoding(false, false); // no BOM in a .NET string 
settings.Indent = true; 
settings.OmitXmlDeclaration = false; 

using (StringWriter textWriter = new StringWriter()) 
{ 
    using (XmlWriter xmlWriter = XmlWriter.Create(textWriter, settings)) 
    { 
     serializer.Serialize(xmlWriter, commRemision, xmlNamespace); 
    } 
} 

和階級結構 -

public class Remision 
{ 
    [XmlElement] 
    public InfoBasica InfoBasica { get; set; } 
    [XmlElement] 
    public Addenda Addenda { get; set; } 
} 

public class InfoBasica 
{ 
    [XmlAttribute] 
    public int folio { get; set; } 
} 

[XmlRoot("Addenda", Namespace = Addenda.ElementNamespace)] 
public class Addenda 
{ 
    public const string ElementNamespace = "http://www.buzonfiscal.com/ns/addenda/bf/2"; 

    [XmlElement(Namespace = ElementNamespace)] 
    public List<AddendaBuzonFiscal> AddendaBuzonFiscal { get;set;} 
} 

public class AddendaBuzonFiscal 
{ 
    [XmlElement(ElementName = "Emisor")] 
    public AddendaEmisor Emisor { get; set; } 

    [XmlElement] 
    public AddendaReceptor Receptor { get; set; } 

    [XmlElement] 
    public TipoDocumento TipoDocumento { get; set; } 
} 

現在我需要我的XML是這樣的 -

<Remision version="5.2" xmlns="http://www.buzonfiscal.com/ns/xsd/bf/remision/52"> 
<InfoBasica folio="10240" /> 
<Addenda> 
<ns:AddendaBuzonFiscal version="2.0" xmlns:ns="http://www.buzonfiscal.com/ns/addenda/bf/2"> 
<ns:Emisor telefono="8787826600" /> 
<ns:Receptor telefono="1234567" /> 
<ns:TipoDocumento nombreCorto="FAC" /> 
</ns:AddendaBuzonFiscal> 
</Addenda> 
</Remision> 

但無法添加ns元素中的前綴。我所能夠產生是這樣的 -

<Remision version="5.2" xmlns="http://www.buzonfiscal.com/ns/xsd/bf/remision/52"> 
<InfoBasica folio="10240" /> 
<Addenda> 
<AddendaBuzonFiscal version="2.0" xmlns:ns="http://www.buzonfiscal.com/ns/addenda/bf/2"> 
<Emisor telefono="8787826600" /> 
<Receptor telefono="1234567" /> 
<TipoDocumento nombreCorto="FAC" /> 
</AddendaBuzonFiscal> 
</Addenda> 
</Remision> 

請注意,我需要空間前綴只在AddendaBuzonFiscal標籤不在XML的每一個標籤。

請幫忙。

+0

你的第二位o f XML不是*您將生成的內容 - 它將是'AddendaBuzonFiscal'中的'xmlns =「..」',而不是'xmlns:ns =「..」'。鑑於此,這兩者在語義上是相同的。你需要提供原因,爲什麼這不被接受。 –

回答

0

添加前綴別名XmlSerializerNamespaces

xmlNamespace.Add("ns", "http://www.buzonfiscal.com/ns/addenda/bf/2"); 
+0

謝謝..這是一個很大的幫助,但一個小故障。此代碼將第二個名稱空間添加頂部標記(Remission),並且我需要AddendaBuzzonFiscal標記中的名稱空間。 –

+0

現在我的頂部標籤看起來像這樣 - 和底部的一個這樣的 -

+0

@AbhinavSingh - 解析器無關緊要,其中聲明的名稱空間。 –

0

你應該有,而不是依靠XmlSerializer這樣一個命名空間聲明一樣,你自己的XML序列化邏輯:

public abstract class XmlSerializationBase : IXmlSerializable 
{ 
    public XmlSchema GetSchema() 
    { 
     return default(XmlSchema); 
    } 

    public virtual void ReadXml(XmlReader reader) 
    { 
     throw new NotImplementedException(); 
    } 

    public virtual void WriteXml(XmlWriter writer) 
    { 
     throw new NotImplementedException(); 
    } 
} 

public class InfoBasica 
{ 
    public string Folio { get; set; } 
} 

[XmlRoot(ElementName = "Emisor")] 
public class Emisor 
{ 
    [XmlAttribute(AttributeName = "telefono")] 
    public string Telefono { get; set; } 
} 

[XmlRoot(ElementName = "Receptor")] 
public class Receptor 
{ 
    [XmlAttribute(AttributeName = "telefono")] 
    public string Telefono { get; set; } 
} 

[XmlRoot(ElementName = "TipoDocumento")] 
public class TipoDocumento 
{ 
    [XmlAttribute(AttributeName = "nombreCorto")] 
    public string NombreCorto { get; set; } 
} 

[XmlRoot(ElementName = "AddendaBuzonFiscal", Namespace = "http://www.buzonfiscal.com/ns/addenda/bf/2")] 
public class AddendaBuzonFiscal 
{ 
    [XmlElement(ElementName = "Emisor", Namespace = "http://www.buzonfiscal.com/ns/addenda/bf/2")] 
    public Emisor Emisor { get; set; } 

    [XmlElement(ElementName = "Receptor", Namespace = "http://www.buzonfiscal.com/ns/addenda/bf/2")] 
    public Receptor Receptor { get; set; } 

    [XmlElement(ElementName = "TipoDocumento", Namespace = "http://www.buzonfiscal.com/ns/addenda/bf/2")] 
    public TipoDocumento TipoDocumento { get; set; } 

    [XmlAttribute(AttributeName = "version")] 
    public string Version { get; set; } 
} 

public class Addenda : XmlSerializationBase 
{ 
    public AddendaBuzonFiscal AddendaBuzonFiscal { get; set; } 

    public override void WriteXml(XmlWriter writer) 
    { 
     writer.WriteStartElement("Addenda"); 

     var serializer = new XmlSerializer(typeof(AddendaBuzonFiscal)); 
     var namespaces = new XmlSerializerNamespaces(); 
     namespaces.Add("ns", "http://www.buzonfiscal.com/ns/addenda/bf/2"); 
     serializer.Serialize(writer, AddendaBuzonFiscal, namespaces); 

     writer.WriteEndElement(); 
    } 
} 

public class Remision : XmlSerializationBase 
{ 
    public InfoBasica InfoBasica { get; set; } 

    public Addenda Addenda { get; set; } 

    public string Version { get; set; } 

    public override void WriteXml(XmlWriter writer) 
    { 
     writer.WriteStartElement("Remision", "http://www.buzonfiscal.com/ns/xsd/bf/remision/52"); 
     writer.WriteAttributeString("version", Version); 
     writer.WriteStartElement("InfoBasica"); 

     writer.WriteAttributeString("folio", InfoBasica.Folio); 
     writer.WriteEndElement(); 

     Addenda.WriteXml(writer); 

     writer.WriteEndElement(); 
    } 
} 

,並生成XML的代碼不再使用序列化程序:

 var settings = new XmlWriterSettings(); 
     settings.Encoding = new UnicodeEncoding(false, false); // no BOM in a .NET string 
     settings.Indent = true; 
     settings.OmitXmlDeclaration = false; 

     var commRemision = new Remision 
     { 
      Version = "5.2", 
      InfoBasica = new InfoBasica { Folio = "10240" }, 
      Addenda = new Addenda 
      { 
       AddendaBuzonFiscal = new AddendaBuzonFiscal 
       { 
        Version = "2.0", 
        Emisor = new Emisor { Telefono = "8787826600" }, 
        Receptor = new Receptor { Telefono = "1234567" }, 
        TipoDocumento = new TipoDocumento { NombreCorto = "FAC" } 
       } 
      } 
     }; 

     using(var stream = new MemoryStream()) 
     { 
      using(var writer = new StreamWriter(stream)) 
      { 
       using (XmlWriter xmlWriter = XmlWriter.Create(writer, settings)) 
       { 
        commRemision.WriteXml(xmlWriter); 
       } 
       stream.Position = 0; 
       using (var reader = new StreamReader(stream)) 
       { 
        Console.WriteLine(reader.ReadToEnd()); 
       } 
      } 
     } 

     Console.ReadLine(); 
相關問題