需要仅在一个XML元素中添加XML名称空间前缀

时间:2016-11-17 08:10:41

标签: c# xml xml-namespaces xmlnode nsxmlelement

我正在尝试使用XMLSerializer在少数几个元素中生成带有名称空间前缀的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的每个标记。

请帮忙。

2 个答案:

答案 0 :(得分:0)

XmlSerializerNamespaces

添加前缀别名
xmlNamespace.Add("ns", "http://www.buzonfiscal.com/ns/addenda/bf/2");

答案 1 :(得分:0)

您应该拥有自己的xml序列化逻辑,而不是依赖于XmlSerializer这样的命名空间声明:

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