带有C#对象的前缀和命名空间

时间:2015-12-11 15:46:05

标签: c# xml

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

我遇到很大困难的部分是为子根元素的子元素添加名称空间前缀,因此在这种情况下,仅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 Desired:

<?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>

1 个答案:

答案 0 :(得分:4)

正如评论中所提到的,差异的原因是contact应位于名称空间http://foo.co.uk中,而不是http://foo.co.uk/Contact

另外,还有一些评论:

  • 可能不需要DataMember属性,除非您在其他地方使用DataContractSerializer
  • 此处的大多数Xml*属性都是多余的,可以通过继承XmlRoot来删除或合并。
  • 如果您只需要XML字符串,那么最好将其序列化为StringWriter而不是流,然后加载到DOM中以获取文本(请参阅{{ 3}}如果您需要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 question