question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Headers not being serialized correctly

See original GitHub issue

Hello, thank you for bringing SOAP to .NET Core! I am having a problem with [XmlAttribute] and [XmlElement] not being honored when serializing headers. Let’s say I have the following MyInterface class:

[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.newtrade.com/expedia/R14/header")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.newtrade.com/expedia/R14/header", IsNullable = false)]
public class MyInterface
{
    private string nameField;
    private decimal versionField;

    [System.Xml.Serialization.XmlAttributeAttribute("Name")]
    public string Name
    {
        get { return this.nameField; }
        set { this.nameField = value;}
    }

    [System.Xml.Serialization.XmlElement("Version")]
    public decimal Version
    {
        get { return this.versionField; }
        set { this.versionField = value; }
    }
}

I am returning the same MyInterface object in a ResponseModel as both a header and a body:

[MessageContract(IsWrapped = false)]
public class ResponseModel
{
        [MessageHeader(Name = "MyInterface", Namespace = "http://www.newtrade.com/expedia/R14/header")]
        public MyInterface InterfaceAsHeader { get; set; }

        [MessageBodyMember(Name = "MyInterface", Namespace = "http://www.newtrade.com/expedia/R14/header")]
        public MyInterface InterfaceAsBody { get; set; }
}

This is the serialized result:

<s:Header>
    <MyInterface xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.newtrade.com/expedia/R14/header">
        <nameField xmlns = "http://schemas.datacontract.org/2004/07/MyContract" > ExpediaDirectConnect </ nameField >
        <versionField xmlns = "http://schemas.datacontract.org/2004/07/MyContract" > 0 </ versionField >
    </ MyInterface >
</ s:Header>
<s:Body>
    <MyInterface Name = "ExpediaDirectConnect" xmlns="http://www.newtrade.com/expedia/R14/header">
        < Version > 0 </ Version >
    </ MyInterface >
</ s:Body>

Note that in the header the element names are the private member names vs those specified in [XmlAttribute] and [XmlElement]. Also Name is being serialized as an element but it has been specified as an [XmlAttribute]. The body is perfect, though.

Can you shed some light on this for me, please? Thanks!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
mdailorcommented, Jun 19, 2020

BOOM! That was exactly what I needed, working perfectly now and thanks so much.

For anyone else who lands here, the class now looks like this:

public class MyInterface : IXmlSerializable
{
    public string Name { get; set; }

    public decimal Version { get; set; }

    public XmlSchema GetSchema() { return null; }
    public void ReadXml(XmlReader reader) { }
    public void WriteXml(XmlWriter writer)
    {
        writer.WriteAttributeString("Name", Name);
        writer.WriteAttributeString("Version", Version.ToString());
    }
}

and the output is:

<MyInterface Name = "ExpediaDirectConnect" Version="0">
</MyInterface>
0reactions
mdailorcommented, Jun 19, 2020

@kotovaleksandr That looks very promising, I will code it up and report back! Thank you!!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Soapheader cannot be serialized
SoapHeader with DataContractAttribute or SerializableAttribute, or removing them from the derived type.' it kinda wants soapheader to be also ...
Read more >
The [Serializable] header is not necessary for class JSON ...
Many tutorials show the [Serializable] header on classes that will be serialized, but I am successfully using the JsonUtilitiy.
Read more >
unable to deserialize value httpheader
I found the correct HttpHeader which is getting serialized correctly. Below is the header value that I am passing in. { "$type":"SourceCode.
Read more >
Serialization - Code Structure
Using this library entails including headers listed in this section. It should not be necessary to explictly include any other header files.
Read more >
How to customize character encoding with System.Text.Json
To serialize all language sets without escaping, use UnicodeRanges.All. Serialize specific characters. An alternative is to specify individual ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found