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.
Related
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.
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
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.
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.
I'd like to convert an external XML document without any XSD schema associated with it into a fluent .NET object.
I have a simple XML file like:
<application>
<parameters>
<param></param>
<param></param>
</parameters>
<generation />
<entities>
<entity ID="1">
<PropTest>Test</PropTest>
</entity>
<entity ID="2">
<PropTest>Another Test</PropTest>
</entity>
</entities>
</application>
I'd like to navigate the document like:
var xConfig = new XmlConfig("file.xml");
// testValue should be assigned "Test"
string testValue = xConfig.Generation.Entities.Entity(1).PropTest;
What is the best way to achieve this in .NET 3.5?
Arguably, the best way to do this these days is with Linq to XML. It is a whole lot easier than messing with XSDs and convoluted class definitions.
XDocument doc = XDocument.Load("file.xml");
var val = doc
.Descendants("entity")
.Where(p => p.Attribute("ID").Value == "1")
.Descendants("PropTest")
.FirstOrDefault();
if (val != null)
Console.WriteLine(val.Value);
The sample file.xml that I used was:
<?xml version="1.0" encoding="utf-8" ?>
<application>
<parameters>
<param></param>
<param></param>
</parameters>
<generation>
<entities>
<entity ID="1">
<PropTest>Test</PropTest>
</entity>
<entity ID="2">Another Test</entity>
</entities>
</generation>
</application>
I just noticed that Lusid also wrote about Linq to SQL while I was writing my answer, but he used XDocument.
Here is my version (file.xml is the XML given in the question):
string testValue =
(string) XElement.Load("file.xml")
.Element("entities")
.Elements("entity")
.FirstOrDefault(entity => entity.Attribute("ID")
.Value == "1") ?? string.Empty;
I would look at xsd.exe. You can also go here for a more informative tutorial on how to use it.
Basically you will be create .NET class equivalents of your XSD. You will then be able to serialize and deserialize your XML and objects.
Kevin Hazzard has written a fluent XML interface for C# using the .NET 4.0 dynamic type part 1 and part 2. This would allow code like:
var v = dx.application.entities.entity[0].PropTest.Value;
If you just extract the schema(which should be easy with xsd.exe) then this online tool (which can also be downloaded) could help you, I tried it and it's ok.
Define classes derived from ConfigurationSection/ConfigurationElement etc. They map to xml files nicely (that's what they'r built for) See MSDN for this. Another way is to create POCO objects and set XML serialization attributes over properties then use System.XML.XMLSerializer to serialize/deserialize to/from XML. See MSDN for XML Serialization.