Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a api which returns the result:


XML
<envelope:envelope xmlns:envelope="http://xxx.com/data-hub/envelope">
<envelope:header>
<additionalInfo>
<Rebates/>
<Expenses>930879969</Expenses>
<Months>20</Months>
</additionalInfo>
</envelope:header>
</envelope>


I am getting issue :

Error in line 1 position 76. Expecting element 'Envelope' from namespace 'http://schemas.datacontract.org/2004/07/'
.. Encountered 'Element' with name 'envelope', namespace 'http://xxx.com/data-hub/envelope'.

What I have tried:

C#
using (HttpClient client = new HttpClient())
{
    var responseTask = await client.GetAsync(uri);
    responseTask.Wait();

    var result = responseTask.Result;
    if (result.IsSuccessStatusCode)
    {
        var readTask = result.Content.ReadAsAsync<t>();
        readTask.Wait();
        response = readTask.Result;
    }


I am getting error at ReadAsynch<t>

The class T i am sending is:

C#
[XmlRoot(ElementName = "envelope", Namespace = "http://xxx.com/data-hub/envelope")]
public class Envelope
{
    [XmlElement(ElementName = "header", Namespace = "http://xxx.com/data-hub/envelope")]
    public Header Header { get; set; }

    [XmlAttribute(AttributeName = "envelope", Namespace = "http://www.w3.org/2000/xmlns/")]
    public string _envelope { get; set; }
}




C#
public class Header
{
    [XmlElement(ElementName = "additionalInfo")]
    public AdditionalInfo AdditionalInfo { get; set; }
}



C#
public class AdditionalInfo
{
    [XmlElement(ElementName = "Rebates")]
    public string Rebates { get; set; }
    [XmlElement(ElementName = "Expenses")]
    public string Expenses { get; set; }
    [XmlElement(ElementName = "Months")]
    public string Months{ get; set; }
}
Posted
Updated 1-Sep-17 14:14pm
v3
Comments
PIEBALDconsult 1-Sep-17 19:52pm    
Looks inconsistent.
Shouldn't the final line of the XML be </envelope:envelope> ?
And in your code, why [XmlAttribute(AttributeName = "envelope", Namespace = "http://www.w3.org/2000/xmlns/")] ?
I have found XML namespaces difficult to work with, and particularly if you mess with the default/empty namespace, so I recommend against it.
Graeme_Grant 1-Sep-17 21:46pm    
"I have found XML namespaces difficult to work with" ... really? Then you would not like XAML programming?
PIEBALDconsult 1-Sep-17 21:49pm    
Which I have no interest in anyway.
Graeme_Grant 1-Sep-17 21:53pm    
WPF, UWP, Xamarin Forms XAML is very flexible & powerful for UI design... and especially for data binding when compared with Winform apps.
PIEBALDconsult 1-Sep-17 22:04pm    
I don't do UI design (I've been asked not to) and data binding is for sissies.

1 solution

As PIEBALDconsult mentions, there is at least one error in your XML with the closing tag:
XML
<envelope:envelope xmlns:envelope="http://xxx.com/data-hub/envelope">
	<envelope:header>
		<additionalInfo>
			<Rebates/>
			<Expenses>930879969</Expenses>
			<Months>20</Months>
		</additionalInfo>
	</envelope:header>
</envelope:envelope>

Also, you are using mixed namespaces. There are two solutions:

1. add an additional namespace definition to the opening element:
XML
<envelope:envelope xmlns:envelope="http://xxx.com/data-hub/envelope" 
                   xmlns="http://xxx.com/data-hub/envelope">
	<envelope:header>
		<additionalInfo>
			<Rebates/>
			<Expenses>930879969</Expenses>
			<Months>20</Months>
		</additionalInfo>
	</envelope:header>
</envelope:envelope>

2. All elements have the same namespace:
XML
<envelope:envelope xmlns:envelope="http://xxx.com/data-hub/envelope">
	<envelope:header>
		<envelope:additionalInfo>
			<envelope:Rebates/>
			<envelope:Expenses>930879969</envelope:Expenses>
			<envelope:Months>20</envelope:Months>
		</envelope:additionalInfo>
	</envelope:header>
</envelope:envelope>

Lastly, the Envelope class should be:
C#
[XmlRoot(ElementName = "envelope", Namespace = "http://xxx.com/data-hub/envelope")]
public class Envelope
{
    [XmlElement(ElementName = "header")]
    public Header Header { get; set; }
}

And here are the other classes, used for testing, for consistancy:
C#
public class Header
{
	[XmlElement(ElementName = "additionalInfo")]
	public AdditionalInfo AdditionalInfo { get; set; }
}

public class AdditionalInfo
{
	[XmlElement(ElementName = "Rebates")]
	public string Rebates { get; set; }
	[XmlElement(ElementName = "Expenses")]
	public string Expenses { get; set; }
	[XmlElement(ElementName = "Months")]
	public string Months { get; set; }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900