自定义的 xml 序列化

本文关键字:序列化 xml 自定义 | 更新日期: 2023-09-27 18:34:35

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

 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>

问候桑吉塔

自定义的 xml 序列化

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

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

编辑:请注意,您需要将具有 XmlNamespaceDeclarations 属性的其他 xmlns 成员添加到您的 User1 类,以便您可以在序列化期间指定实际的命名空间。

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

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();
相关文章: