My problem is with deserialization of xml to c# objects. I have some class derived from some other class (I have the reason why I need to use inheritance in this place - doesn't matter why):
[Serializable]
[XmlType(TypeName = "OTA_HotelResRQ")]
public class ResRQ : OTA_HotelResRQ
{
}
in OTA_HotelResRQ I have declared namespaces and other information:
[Serializable]
[XmlTypeAttribute(Namespace = "http://www.opentravel.org/OTA/2003/05")]
[XmlRootAttribute(Namespace = "http://www.opentravel.org/OTA/2003/05", IsNullable = false)]
public class OTA_HotelResRQ : OtaRequestMessage, IRequest
And when I'm trying to serialize some request which looks like below:
<ns:OTA_HotelResRQ xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ns="http://www.opentravel.org/OTA/2003/05"
PrimaryLangID="en" EchoToken="5613971064477293649" ResStatus="Commit" Version="2.1">
...SOME REQUEST WITH NS:....
<ns:POS><ns:/POS>
And now when I'm trying to deserialize this I have:
There is an error in XML document (3, 2). ---> System.InvalidOperationException: <OTA_HotelResRQ xmlns='http://www.opentravel.org/OTA/2003/05'> was not expected.
do you have some idea why I can't deserialize this? I can't modify base class for my model and also I need to have "ns" prefixes because service where I want to send that requires this format.
UPDATE:
My deserialization is implemented, that I'm getting bytes from string and try to deserialize using:
return (T) new XmlSerializer(typeof (T)).Deserialize(new MemoryStream(bytes));
I tried to fix it using solution provided by Charles and I updated my model:
[Serializable]
[XmlRoot("OTA_HotelResRQ", Namespace = "http://www.opentravel.org/OTA/2003/05")]
[XmlType("OTA_HotelResRQ", Namespace = "http://www.opentravel.org/OTA/2003/05")]
public class ResRQ : OTA_HotelResRQ
{
}
but still with no success. When I try to deserialize then I'm getting exception:
Types 'OTA_HotelResRQ' and
'ResRQ' both use the XML type name,
'OTA_HotelResRQ', from namespace 'http://www.opentravel.org/OTA/2003/05'. Use
XML attributes to specify a unique XML name and/or namespace for the type.
XML attributes are not inherited, so you need to add an XmlRoot attribute to your derived class:
[Serializable]
[XmlRoot("OTA_HotelResRQ", Namespace = "http://www.opentravel.org/OTA/2003/05")]
public class ResRQ : OTA_HotelResRQ
{
}
Related
I have a situation where I have a REST controller that is called by a user and this controller then requests data from upstream and receives JSON as a response. This JSON is then transformed into XML and send back to the user as a response.
The problem is that I am not able to set specific namespace for the XML root element. I am using DataContractSerializer.
I am not really experienced with .NET and have previously worked mainly with JSON so i am a bit lost as to what to try next.
I have tried to set the namespace using ContractNamespaceAttribute like:
[assembly: ContractNamespaceAttribute("http://schemas.datacontract.org/2004/07/APIBridge.Models", ClrNamespace = "APIBridge.Models")]
namespace APIBridge.Models
{
[DataContract]
public class Order
{
// DataMembers here...
}
}
And I also tried setting the namespace in the DataContracAttribute like:
namespace APIBridge.Models
{
[DataContract(Name = "Order", Namespace =
"http://schemas.datacontract.org/2004/07/APIBridge.Models")]
public class Order
{
// Datamembers here...
}
}
How I would like the namespace to be set is:
<ArrayOfOrder xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/APIBridge.Models">
But the actual result is:
<ArrayOfOrder xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
https://learn.microsoft.com/en-us/dotnet/api/system.runtime.serialization.datacontractattribute?redirectedfrom=MSDN&view=netframework-4.8
In the DataContractAttribute documentation above it says:
"By default, when you apply the DataContractAttribute to a class, it uses the class name as the local name and the class's namespace (prefixed with "http://schemas.datacontract.org/2004/07/") as the namespace URI."
This actually would be the desired result but also as a default namespace I get the same result that is mentioned above. Why is this?
Any ideas?
UPDATE: Below requested service operation
public List<Order> loadOrdersBasic(UserOptions userOpts)
{
List<Order> orders = new List<Order>();
HttpClient httpClient = AuthenticationHelper.CreateHttpClient(userOpts, options);
String url = String.Format("api/orders?supplier_no={0}", userOpts.SupplierId);
HttpResponseMessage response = httpClient.GetAsync(url).Result;
if (response.IsSuccessStatusCode)
{
orders = response.Content.ReadAsAsync<List<Order>>().Result;
}
else {
throw new ServiceException(getHttpErrorMessage(url, response));
}
return orders;
}
Answering to my own question.
Turned out that I had missed one line in my configuration file where XmlMediaTypeFormatter was set to use XmlSerializer instead of DataContractSerializer. This overrode the default namespace.
I looked for an issue but couldn't find the answer the this particular case, so here is the issue.
When trying to deserialize a xml string into a strong type object I got the following message:
System.InvalidOperationException: There is an error in XML document (1, 2). ---> System.InvalidOperationException: <GetPointOfDelivery_Out xmlns='urn:webbeB2B:webservices:v0'> was not expected.
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderGetPointOfDelivery_Out.Read16_GetPointOfDelivery_Out()
Although the class has to my opinion the correct namespace attribute value defined, being :
[XmlType(AnonymousType = true, Namespace = "urn:webbeB2B:webservices:v0")]
public class GetPointOfDelivery_Out
{
[XmlElement("POD")]
public POD[] POD
….
}
The source string looks like this:
<out:GetPointOfDelivery_Out xmlns:out="urn:webbeB2B:webservices:v0">
<out:POD>
<out:PODID>FT}UntwKNFlX0000h100Dm</out:PODID>
….
</out:POD>
<out:ErrorMessage>
<out:MsgType>S</out:MsgType>
</out:ErrorMessage>
</out:GetPointOfDelivery_Out>
I use the following code to deserialize the string
var xmlSerializer = new XmlSerializer(typeof(T));
using (var textReader = new StringReader(xml))
{
return (T)xmlSerializer.Deserialize(textReader);
}
(Where T is GetPointOfDelivery_Out and xml the example given above.)
Any help is welcome.
While looking somewhat deeper into stack overflow I found the answer in the following post:
https://stackoverflow.com/a/1232328/1145146
I added the XmlRoot attribute on the target class to be deserialized
[Serializable]
[XmlRoot("GetPointOfDelivery_Out", Namespace = "urn:webbeB2B:webservices:v0")]
[XmlType(AnonymousType = true, Namespace = "urn:webbeB2B:webservices:v0")]
public class GetPointOfDelivery_Out
I am receiving some XML that looks like this.
<?xml version="1.0"?>
<parent xmlns="urn:place-com:spec.2004">
<childA xmlns="">123</childA>
<childB xmlns="">456</childB>
</parent>
I'm deserializing it into a class with C#'s XmlSerializer. It works, except the blank child namespaces are giving me trouble. Their properties in the class are null.
I understand the blank namespace puts the element in the global namespace. Probably not what is intended, but what I am receiving none the less.
If I manually delete the xmlns="" attribute from the child element it works. If I fill out the attribute with xmlns="testNamespace" and add the following attribute to the property in the class, it works.
[XmlElement(Namespace="testNamespace")]
public string childA
{ ... }
However, leaving the XML as is, and adding the following attribute does not work.
[XmlElement(Namespace="")]
How can I specify that an element has a blank namespace for deserialization?
For the xml presented in question the following class works perfectly.
[XmlRoot("parent", Namespace = "urn:place-com:spec.2004", IsNullable = false)]
public class Parent
{
[XmlElement("childA", Namespace = "")]
public string ChildA { get; set; }
[XmlElement("childB", Namespace = "")]
public string ChildB { get; set; }
}
I am attempting to serialize a class as XML and to have the properties be serialized as attributes of the class, rather than a nested node. I am using WebApi to automatically handle the serialization of the XML.
This is my class:
[DataContract (Namespace="", Name="AttributeTest")]
[Serializable]
public class AttributeTestClass
{
[XmlAttribute("Property")]
[DataMember]
public int Property1 { get; set; }
}
Here is the output I am receiving (note that Property1 is not an attribute in spite of it being decorated with [XmlAttribute]):
<AttributeTest xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Property1>123</Property1>
</AttributeTest>
This is the output I want to receive:
<AttributeTest Property1="123" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
</AttributeTest>
What am I missing?
I'm not familiar with WebApi but the output you receive looks like it's serialized using DataContractSerializer, not XmlSerializer which you would need. Check if adding the following to Application_Start in Global.asax helps:
GlobalConfiguration.Configuration.Formatters.Clear();
GlobalConfiguration.Configuration.Formatters.Add(
new System.Net.Http.Formatting.XmlMediaTypeFormatter());
GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true;
(From http://serena-yeoh.blogspot.de/2013/02/xml-serialization-in-aspnet-web-api.html)
The XML I am getting is provided by an outside source so I don't have the ability to easily reformat it. I would like to use xml attributes on my entities instead of having to write a linq query that knows how the XML and entity is formatted. Here is an example:
<?xml version="1.0"?>
<TERMS>
<TERM>
<ID>2013-2</ID>
<DESC>Spring 2013</DESC>
</TERM>
<TERM>
<ID>2013-3</ID>
<DESC>Summer 2013 Jun&Jul</DESC>
</TERM>
</TERMS>
I know the the XMLSerializer expects ArrayOfTerm instead of TERMS for example, but that I can tweak my entity to use a different element name with the xml attributes such as this:
public class TermData
{
[XmlArray("TERMS")]
[XmlArrayItem("TERM")]
public List<Term> terms;
}
public class Term
{
[XmlElement("ID")]
public string id;
[XmlElement("DESC")]
public string desc;
}
and I am deserializing the data like so:
TermData data;
XmlSerializer serializer = new XmlSerializer(typeof(TermData));
using (StringReader reader = new StringReader(xml))
{
data = (TermData)serializer.Deserialize(reader);
}
return View(data.terms);
The problem I am facing is that TERMS is the root and the array itself. If the XML were to have a root element that was not the array, I could edit my TermData class like so and it would deserialize correctly (already tested).
[XmlRoot("ROOT")]
public class TermData
{
[XmlArray("TERMS")]
[XmlArrayItem("TERM")]
public List<Term> terms;
}
Note that using TERMS as the XMLRoot does not work. Right now, my code is throwing
InvalidOperationException: There is an error in XML document (2,2).
InnerException: "<TERMS xmlns=" was not expected.
This would lead me to believe that the XML is not formatted correctly, but from my understanding the example I gave is perfectly valid XML.
This would all be trivial if I could edit the source xml, but there could be tons of other responses like this and I need to be able to flex for whatever I might get. What I'm trying to confirm is whether or not the XMLSerializer can support this type of XML structure. I've tested just about everything and can't get it deserialize without editing the XML. It would also be convenient if I didn't have to define a wrapper class (TermData) to hold the list, but this seems to only work if the xml follows the naming conventions for the serializer (ArrayOfTerm, etc).
Maybe you can try :
[XmlRoot("TERMS")]
public class TermData
{
public TermData()
{
terms = new List<Term>();
}
[XmlElement("TERM")]
public List<Term> terms{get;set;}
}
public class Term
{
[XmlElement("ID")]
public string id{get;set;}
[XmlElement("DESC")]
public string desc{get;set;}
}
Hope this will help,