Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prefixes and Namespaces with C# Objects

I am attempting to create a POST function that serialises C# class objects into XML.

The part I am having great difficulty with is adding namespace prefixes to the sub-root element's children, so in this instance, contact children only.

The only way I seem to be able to get the prefix onto the child elements of contactis to add them through SerializerNamespace class, however I can only get this to attach to the root element, CreateContact.

How can I achieve this?

XML Currently Produced:

<?xml version=\"1.0\"?>
<CreateContact xmlns:a="http://foo.co.uk/Contact" xmlns="http://foo.co.uk">
<a:contact>
<a:Email>[email protected]</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>

Serialisation function:

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 Class Constructs:

[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>[email protected]</a:Email>
<a:FirstName>Simon</a:FirstName>
<a:LastName>Test</a:LastName>
<a:Phone>09088408501</a:Phone>
<a:Title>Mr</a:Title>
</contact>
</CreateContact>
like image 456
PurpleSmurph Avatar asked Aug 17 '26 00:08

PurpleSmurph


1 Answers

As alluded in the comments, the reason for the difference is that contact should be in the namespace http://foo.co.uk, not http://foo.co.uk/Contact.

As an aside, a couple of further comments:

  • You probably don't need the DataMember attributes, unless you're using DataContractSerializer somewhere else.
  • Most of the Xml* attributes are superfluous here, and could be removed or consolidated by inheriting from XmlRoot.
  • If all you need is the XML string, you'd be better off serialising to something like a StringWriter rather than to a stream and then loading into the DOM just to get the text (see this question if you need the XML declaration to specify utf-8)

So, you'd get the XML as below:

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

With your classes defined as:

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

See this fiddle for the complete example.

like image 110
Charles Mager Avatar answered Aug 18 '26 12:08

Charles Mager



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!