自定义的xml序列化

时间:2016-02-15 12:27:22

标签: c# xml serialization

在我的应用程序中,我将收到一些创建数据的请求,我创建了如下所示的请求类,

 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; }
}

并序列化我在代码下面使用的请求

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;

        }

我没有按照我的预期得到结果,有没有办法自定义序列化以获得以下格式的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>

此致 桑杰塔

1 个答案:

答案 0 :(得分:0)

您可以先在数据类上添加命名空间属性:

[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;
   ...
}

编辑:请注意,您需要向User1类添加一个带有XmlNamespaceDeclarations属性的附加xmlns成员,以便您可以在序列化期间指定实际的命名空间。

然后,将这些名称空间添加到序列化程序方法,如:

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