Ignoring namespaces in XML deserialize - c#

I want to deserialize XML into object. I have no controll what xml i'm reciving, and into what object I'm deserializing.
I tried this: Can I make XmlSerializer ignore the namespace on deserialization? and it worked... almost.
my xml
<request xmlns:a="http://schemas.datacontract.org/2004/07/My.Contracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/">
<a:ProfileInfo i:type="a:ProfileAndBSymbol">
<a:BSymbol>_47</a:BSymbol>
<a:ProfileSymbol>FAJNY</a:ProfileSymbol>
</a:ProfileInfo>
</request>
a:ProfileInfo works fine, but i:type thrown an exception. How to ignore i:?
exception is in Polish and it is like this:
InvalidOperationException: type is invalid. name= ProfileAndBSymbol,
namespace=http://schemas.datacontract.org/2004/07/My.Contracts

Related

Deserialize XML having Multiple xsi:type attributes

I am working on deserializing XML file where a same class has different namespaces. I am getting error while The specified type was not recognized. Here is a snippet from the XML I am trying to parse
<BookingHistory xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns3="dbpnr" xsi:type="ns3:SegmentHistory" Segment="ID13" eventDate="2014-04-03" eventTime="05:29:00" actionType="Change"/>
<BookingHistory xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns3="dbpnr" xsi:type="ns3:SsrHistory" eventDate="2014-04-03" eventTime="14:06:00" actionType="Add">
<BookingHistory xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns3="dbpnr" xsi:type="ns3:RemarkHistory" eventDate="2014-04-03" eventTime="15:02:00" actionType="Add">
Here is the sample XML
https://drive.google.com/file/d/1RDOAGa2TECSIauSHkdYTuSNuTWx5GVb8/view
Any suggestions would be highly appreciated.

Issue with xml to object c# using Xml.Serialization

I have an old xml response and I cannot map it to an object as I normally do.
Using paste special> xml
:When I deserialize it, it's throwing "The specified type was not recognized: name='Array'"
Using class from https://xmltocsharp.azurewebsites.net/
:I can map to object but there is nothing in return property
<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="http://uat.api.sample.uk">
<SOAP-ENV:Body>
<ns1:directReportResponse xmlns:ns1="urn:Direct">
<return xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="tns:DirectResult[1]">
<item xsi:type="tns:DirectResult">
<TMSReference xsi:type="xsd:string">sampleTMS</TMSReference>
<Postcode xsi:type="xsd:string">samplePostcode</Postcode>
<ExpectedDelivery xsi:type="xsd:string">2020-08-25</ExpectedDelivery>
</item>
</return>
</ns1:directReportResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
After follow andyrut, I can map it to custom type but still no value in an item obj
The generated class does not know that a "return" tag is an array of "item" elements. You simply need to make the following modifications to your C# class:
Delete the class definition for Return.
Mark the Return data member as a list/array of Item's. Change the Return data member in the DirectReportResponse to a list and decorate it like so:
[XmlArray(ElementName="return", Namespace="")]
[XmlArrayItem(ElementName="item", Namespace="")]
public List<Item> Return { get; set; }
There's a Type specified in the Item XML, so decorate your Item class by adding the following line above it:
[XmlType(Namespace="http://uat.api.sample.uk", TypeName="DirectResult")]
You may need to tweak your C# class for the other specified types in the XML file.

XmlSyntaxException vs XmlException in .Net

Which is prefer in what case in the concept of reuse predefined exception classes?
For example I was parsing a document using XmlReader and faced with inconsistent close tag:
<?xml version=""1.0"" encoding=""UTF-8""?>
<element1>
<element2>
some content
</element3>
</element1>
What to throw?
It should be noted that this two exceptions do not inherit one another so should be caught separately.

How to do simple XML schema validation with no namespaces in C#

I have generated a set of classes using xsd.exe and created an XML document from the resulting generated code. I would now like to validate the serialized class instance against the original xsd.
My XML is like this:
<?xml version="1.0" encoding="UTF-8"?>
<MyRoot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
-- rest of XML document here
</MyRoot>
My XSD is like this:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="MyRoot" type="MyRootType"/>
-- MyRootType definition and rest of XSD
</xs:schema>
When I try to validate the XML using a XmlReader, I get the following error:
"The 'MyRoot' element is not declared."
What could be wrong?
In your MyRoot element, you need to add the location of the XSD. I would also recommend defining the namespace (unless you have good reason not to).
<api:MyRoot xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:api='http://www.myserver.com/schema'
xsi:schemaLocation='http://www.myserver.com/schema http://www.myserver.com/schema/websuiterecord.xsd'>
</api:MyRoot>
This way, the validation tool knows where to find your XSD to validate your XML against.
The approach was correct, but the XSD was not infact being read. I corrected this and it worked as expected.

Remove namespace from generated XML in .NET [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
XmlSerializer: remove unnecessary xsi and xsd namespaces
I'm generating some XML using XMLSerializer and a class marked up with attributes. This XML is sent to a REST web service.
It generates the following XML:
<?xml version="1.0" encoding="utf-8"?>
<person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<first-name>API</first-name>
<last-name>TestPersonDeleteMe</last-name>
<title>Delete me</title>
</person>
All would be well, except the web service I'm using doesn't understand the schema stuff and throws a 500 error.
Is there a way to stop XmlSerializer adding 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"' to the person tag?
if you use custom serializer try this
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add(string.Empty, string.Empty);
then add namespaces object to your serializer.

Categories

Resources