3

我正在尝试创建一个将 C# 类对象序列化为 XML 的 POST 函数。

我遇到很大困难的部分是将命名空间前缀添加到子根元素的子元素,所以在这种情况下,contact只有子元素。

我似乎能够将前缀添加到子元素的唯一方法contact是通过SerializerNamespace类添加它们,但是我只能将它附加到根元素,CreateContact.

我怎样才能做到这一点?

当前生成的 XML:

<?xml version=\"1.0\"?>
<CreateContact xmlns:a="http://foo.co.uk/Contact" xmlns="http://foo.co.uk">
<a:contact>
<a:Email>stest@gmail.com</a:Email>
<a:FirstName>Simon</a:FirstName>
<a:LastName>Test</a:LastName>
<a:Phone>09088408501</a:Phone>
<a:Title>Mr</a:Title>
</a:contact>
</CreateContact>

序列化功能:

public static void CreateContact(Contact contact)
{
    string tmp = url;
    string xml = "";
    string result = "";

    XmlDocument xd = new XmlDocument();

    var cc = new CreateContact();
    cc.contact = contact;
    var xs = new XmlSerializer(cc.GetType());

    XmlSerializerNamespaces xsn = new XmlSerializerNamespaces();
    xsn.Add("a", "http://foo.co.uk/Contact");

    using (MemoryStream ms = new MemoryStream())
    {
    xs.Serialize(ms, cc, xsn);
    ms.Position = 0;
    xd.Load(ms);
    xml = xd.InnerXml;
    }

    using (WebClient web = new WebClient())
    {
    web.Credentials = new NetworkCredential(username, password);
    web.Headers.Add("Content-Type", "application/xml");
    try
    {
        result = web.UploadString(tmp, "POST", xml);
    }
    catch (WebException ex)
    {
    }
    }
}

XML 类结构:

[Serializable()]
[XmlRoot(ElementName = "CreateContact", Namespace = "http://foo.co.uk")]
public class CreateContact
{
    [XmlElement(ElementName = "contact", Namespace = "http://foo.co.uk/Contact")]
    public Contact contact { get; set; }
}

[DataContract(Name = "Contact", Namespace = "http://foo.co.uk/Contact")]
[XmlType("a")]
public class Contact
{
    [XmlElement(ElementName = "Email", Namespace = "http://foo.co.uk/Contact")]
    [DataMember(Name = "Email")]
    public string Email { get; set; }
    [XmlElement(ElementName = "FirstName", Namespace = "http://foo.co.uk/Contact")]
    [DataMember(Name = "FirstName")]
    public string Firstname { get; set; }
    [XmlElement(ElementName = "LastName", Namespace = "http://foo.co.uk/Contact")]
    [DataMember(Name = "LastName")]
    public string Lastname { get; set; }
    [XmlElement(ElementName = "Phone", Namespace = "http://foo.co.uk/Contact")]
    [DataMember(Name = "Phone")]
    public string Phone { get; set; }
    [XmlElement(ElementName = "Title", Namespace = "http://foo.co.uk/Contact")]
    [DataMember(Name = "Title")]
    public string Title { get; set; }
}

所需的 XML:

<?xml version=\"1.0\"?>
<CreateContact  xmlns="http://foo.co.uk">
<contact xmlns:a="http://foo.co.uk/Contact">
<a:Email>stest@gmail.com</a:Email>
<a:FirstName>Simon</a:FirstName>
<a:LastName>Test</a:LastName>
<a:Phone>09088408501</a:Phone>
<a:Title>Mr</a:Title>
</contact>
</CreateContact>
4

1 回答 1

4

正如评论中提到的,差异的原因是contact应该在 namespacehttp://foo.co.uk中,而不是http://foo.co.uk/Contact.

顺便说一句,还有一些评论:

  • 可能不需要这些DataMember属性,除非您在DataContractSerializer其他地方使用。
  • 大多数Xml*属性在这里都是多余的,可以通过继承来删除或合并XmlRoot
  • 如果您只需要 XML 字符串,最好将其序列化为 aStringWriter而不是流,然后加载到 DOM 中以获取文本(如果您需要指定 XML 声明,请参阅此问题utf-8

所以,你会得到如下的 XML:

var xsn = new XmlSerializerNamespaces();
xsn.Add("a", "http://foo.co.uk/Contact");

var xs = new XmlSerializer(typeof(CreateContact));

using (var stringWriter = new StringWriter())
{
    xs.Serialize(stringWriter, cc, xsn);
    xml = stringWriter.ToString();
}

将您的类定义为:

[XmlRoot(ElementName = "CreateContact", Namespace = "http://foo.co.uk")]
public class CreateContact
{
    [XmlElement(ElementName = "contact")]
    public Contact Contact { get; set; }
}

[XmlRoot("contact", Namespace = "http://foo.co.uk/Contact")]
public class Contact
{
    public string Email { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Phone { get; set; }
    public string Title { get; set; }
}

有关完整示例,请参见this fiddle

于 2015-12-11T16:16:26.143 回答