<...">

XML serialization namespaces

I have a problem with the namespaces that my code produces. I need XML:

<?xml version="1.0" encoding="utf-8"?>
<ClassToSerialize Type="Customer" Name="Some Name" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.123.org/namespace C:\Schema\ClassToSerialize.xsd" 
xmlns:Test="http://www.Test.org/" xmlns="http://www.nrf-arts.org/namespace">
  <Address>
    <Line1>Addr1</Line1>
    <Line2>Addr2</Line2>
  </Address>
</ClassToSerialize>

What I get is XML:

<?xml version="1.0" encoding="utf-8"?>
<ClassToSerialize xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:schemaLocation="http://www.123.org/namespace C:\Schema\ClassToSerialize.xsd"
 xmlns:Test="http://www.Test.org/" xmlns:xmlns="http://www.nrf-arts.org/namespace" Type="Customer" Name="Some Name">
  <Address>
    <Line1>Addr1</Line1>
    <Line2>Addr2</Line2>
  </Address>
</ClassToSerialize>

The main differences are as follows:

1. xmlns:schemaLocation= needs to be xsi:schemaLocation=
2. xmlns:xmlns= needs to be xmlns=
3. Attributes Order, I would prefer the Attributes to be presented before the namespace attributes (This is not a big Issue, just nice to have)

Currently what I am doing is replacing the values ​​in the serialized line 1 and 2 above with the values ​​I want, a hack that works, but I suspect there is a way to change the namespace code to do this at this point

Here is the code I'm using, how can I change GetNameSpace () to do what I need at points 1 and 2, can I also redefine the attributes ?:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        ClassToSerialize myInstance = new ClassToSerialize();
        myInstance.Type = "Customer";
        myInstance.Name = "Some Name";
        myInstance.AddressField = new Address("Addr1", "Addr2");

        String sString = SerializeObject<ClassToSerialize>(myInstance, GetNameSpace());

        //Hack to achieve what I want from namespaces
        sString = sString.Replace("xmlns:schemaLocation=", "xsi:schemaLocation=");
        sString = sString.Replace("xmlns:xmlns=", "xmlns=");
    }

    private XmlSerializerNamespaces GetNameSpace()
    {
        XmlSerializerNamespaces xsNS = new XmlSerializerNamespaces();

        xsNS.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
        xsNS.Add("xmlns", "http://www.nrf-arts.org/namespace");
        xsNS.Add("schemaLocation", "http://www.123.org/namespace C:\\Schema\\ClassToSerialize.xsd");
        xsNS.Add("Test", "http://www.Test.org/");

        return xsNS;
    }

    public static string SerializeObject<X>(X toSerialize, XmlSerializerNamespaces xmlNameSpace)
    {
        string strRetVal = "";
        try
        {
            XmlSerializer xmlSerializer = new XmlSerializer(toSerialize.GetType());
            StringWriter textWriter = new StringWriter();

            using (StringWriter writer = new Utf8StringWriter())
            {
                xmlSerializer.Serialize(writer, toSerialize, xmlNameSpace);
                strRetVal = writer.ToString();
            }
        }
        catch (Exception ex)
        {
            string strError = ex.ToString();
        }

        return strRetVal;
    }
}

public class Utf8StringWriter : StringWriter
{
    public override Encoding Encoding
    {
        get { return Encoding.UTF8; }
    }
}

    public class ClassToSerialize
{
    [XmlAttribute()]
    public string Type { get; set; }

    [XmlAttribute()]
    public string Name { get; set; }

    [XmlElement("Address")]
    public Address AddressField { get; set; }
}

public class Address
{
    [XmlElement, DefaultValue("")]
    public string Line1 { get; set; }

    [XmlElement, DefaultValue("")]
    public string Line2 { get; set; }

    public Address()
    {

    }

    public Address(string L1, string L2)
    {
        Line1 = L1;
        Line2 = L2;
    }
}
+4
source share
1 answer

, . . GetNameSpace() ClassToSerialize GetAdditionalNamespaces():

[XmlRoot("ClassToSerialize", Namespace="http://www.nrf-arts.org/namespace")]
public class ClassToSerialize
{
    public static XmlSerializerNamespaces GetAdditionalNamespaces()
    {
        XmlSerializerNamespaces xsNS = new XmlSerializerNamespaces();

        xsNS.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
        xsNS.Add("Test", "http://www.Test.org/");

        return xsNS;
    }

    [XmlAttribute("schemaLocation", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
    public string XSDSchemaLocation
    {
        get
        {
            return "http://www.123.org/namespace C:\\Schema\\ClassToSerialize.xsd";
        }
        set
        {
            // Do nothing - fake property.
        }
    }

    [XmlAttribute()]
    public string Type { get; set; }

    [XmlAttribute()]
    public string Name { get; set; }

    [XmlElement("Address")]
    public Address AddressField { get; set; }
}
+5

Source: https://habr.com/ru/post/1568015/


All Articles