简体   繁体   中英

Customized xml serialization

In my application, i will be receiving some request to create data and i creates request class as below,

 public class SaveUser
{
    public string Comments { get; set; }
    public int CustomerNumber { get; set; }
    public string Password { get; set; }
    public string Username { get; set; }
    public User1 User { get; set; }

}

public class User1
{
    public string City { get; set; }
    public string Country { get; set; }
    public string Email { get; set; }
    public string MobileNumber { get; set; }
    public string Name { get; set; }
    public string Password { get; set; }
    public string StreetAddress { get; set; }
    public int UserId { get; set; }
    public string Username { get; set; }
    public string ZipCode { get; set; }
}

And to serialize the request i used below code,

public static string SerializeToXml<T>(T obj)
        {

            // out this extraneous xml.
            // StringWriter Output = new StringWriter(new StringBuilder());
            string result;
            XmlSerializer ser = new XmlSerializer(typeof(T));

            using (StringWriter Output = new StringWriter(new StringBuilder()))
            {
                ser.Serialize(Output, obj);
                result = Convert.ToString(Output);
            }
            return result;

        }

I am not getting result as i expected, Is there any way to customize the serialization to get the below format of xml,

<SaveUser xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.mywebsite.no/webservice/types">
  <Comments>String</Comments>
  <CustomerNumber>222</CustomerNumber>
  <Password>test</Password>
  <Username>Sangeetha</Username>
  <User xmlns:d2p1="http://schemas.datacontract.org/2004/07/mywebsite.Engine.Engine.Types">
    <d2p1:City>String</d2p1:City>
    <d2p1:Country>String</d2p1:Country>
    <d2p1:CustomerNumber>0</d2p1:CustomerNumber>
    <d2p1:Email>test.mp@gmail.com</d2p1:Email>
    <d2p1:MobileNumber>91-1234567890</d2p1:MobileNumber>
    <d2p1:Name>Sangeetha</d2p1:Name>
    <d2p1:Password>test</d2p1:Password>
    <d2p1:StreetAddress>String</d2p1:StreetAddress>
    <d2p1:UserId>0</d2p1:UserId>
    <d2p1:Username>Sangeetha</d2p1:Username>
    <d2p1:ZipCode>560068</d2p1:ZipCode>
  </User>
</SaveUser>

Regards Sangeetha

You could first add namespace attributes on your data classes:

[XmlRoot(Namespace = "http://www.mywebsite.no/webservice/types")]
public class SaveUser
{
   ...
}

[XmlType(Namespace = "http://schemas.datacontract.org/2004/07/mywebsite.Engine.Engine.Types")]
public class User1
{
   [XmlNamespaceDeclarations]
   public XmlSerializerNamespaces xmlns;
   ...
}

EDIT : Please note that you need to add an additional xmlns member with XmlNamespaceDeclarations attribute to your User1 class so that you can specify the actual namespace during serialization.

Then, add these namespaces to the serializer method like:

SaveUser user = new SaveUser...; // create SaveUser object
user.User = new User1...; //create User1 object
// **EDIT**: instantiate and add your namespace to User1 here...
user.User.xmlns = new XmlSerializerNamespaces();
user.User.xmlns.Add("d2p1", "http://schemas.datacontract.org/2004/07/mywebsite.Engine.Engine.Types");

XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "http://www.mywebsite.no/webservice/types");
ns.Add("i", "http://www.w3.org/2001/XMLSchema-instance");

XmlSerializer s = new XmlSerializer(typeof(SaveUser));
StreamWriter w = new StreamWriter("Your XML File");
s.Serialize(w, user, ns);
w.Close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM