Serializing XML in C# .NET - c#

I have the following scenarios for output from a web service (the output's type is string, but the content of the string is XML).
When the service didn't succeed the output is:
<root>
<exec_id>2053c884-beec-4c33-af64-bce8c4a8601c</exec_id>
<ERROR_CODE>-13</ERROR_CODE>
<OUTPUT_XML>Policy GO Period is mistaken (Mar 9 2016 8:43PM, Mar 6 2017 11:59PM)</OUTPUT_XML>
</root>
When the service returns something, the response is:
<root>
<exec_id>9a506024-8996-4f17-bcc4-a4616b2c28da</exec_id>
<ERROR_CODE>0</ERROR_CODE>
<OUTPUT_XML>
<root>
<ProposalNo>000334374</ProposalNo>
<InsPremium>225.40</InsPremium>
<InsDuePremium>241.310</InsDuePremium>
<FeesInfo>
<Fee>
<PaymentDate>2016-03-09T16:08:34.613</PaymentDate>
<FeeSum>241.31</FeeSum>
<FeeSumCurCode>BGN</FeeSumCurCode>
<AgencyCode>001</AgencyCode>
</Fee>
</FeesInfo>
<ERROR_MSG>Успешно изпълнена процедура</ERROR_MSG>
</root>
</OUTPUT_XML>
</root>
So, based on this, I created a base class that contains exec_id and ERROR_CODE, because they are common for all responses. Now the problem is OUTPUT_XML, because it's a different object every time so I have a successor class for each method of the service. The problem is when the service returns an error message in the OUTPUT_XML. My question is how to handle this in the best possible way?

Is the service under your control? (Not sure if that's a constraint.)
There are no attributes that will allow out-of-the-box deserialization of the XML in both scenarios above. You could write some custom serialization but that would be overkill.
If the service is yours then you could define a response class and serialize that. If you can serialize it then you can deserialize it. Then in your response XML you have an element for the output and an element for an error message, not one element which may contain either. The contents of XML should be more predictable. That's why we use schemas. But using a class to serialize and deserialize accomplishes the same thing. The XML will be predictable.
You could also get the InnerXml property of the <OUTPUT_XML> node and if it begins with <root> then deserialize that string is a separate XML document. That's logical since that's what they're actually giving you - an XML document as string inside of an XML document.
Or, as was said, if the error code is reliable indicator then you could just read that first.

Related

How i Can Read XML Element and Attribute Values from Below Example

Below Example How can Get "Airtel" and "145" Values, because my Client Has given this type XML Response
So How Can I Get both Values
<item item="Campaign name" type="string">Airtel</item>
<item item="Daily Limit" type="number">145</item>
As per the comment, we would need to see the full XML to give you a more complete answer. But you have 3 real options :
XMLReader. Probably not what you want as it's forward only and involves a lot of manual parsing.
XMLDocument. Not a bad option if you only want limited amount of notes and want their values and don't want to deserialize the entire XML doc.
XMLSerializer. Good if you want to deserialize the entire object straight from XML to class without you having to do a heck of a lot.
If you edit your question to include the full XML doc, then I can give you a more complete answer or you can read about your options for parsing XML here : https://dotnetcoretutorials.com/2020/04/23/how-to-parse-xml-in-net-core/

Convert string to valid XML [duplicate]

Currently, I'm working on a feature that involves parsing XML that we receive from another product. I decided to run some tests against some actual customer data, and it looks like the other product is allowing input from users that should be considered invalid. Anyways, I still have to try and figure out a way to parse it. We're using javax.xml.parsers.DocumentBuilder and I'm getting an error on input that looks like the following.
<xml>
...
<description>Example:Description:<THIS-IS-PART-OF-DESCRIPTION></description>
...
</xml>
As you can tell, the description has what appears to be an invalid tag inside of it (<THIS-IS-PART-OF-DESCRIPTION>). Now, this description tag is known to be a leaf tag and shouldn't have any nested tags inside of it. Regardless, this is still an issue and yields an exception on DocumentBuilder.parse(...)
I know this is invalid XML, but it's predictably invalid. Any ideas on a way to parse such input?
That "XML" is worse than invalid – it's not well-formed; see Well Formed vs Valid XML.
An informal assessment of the predictability of the transgressions does not help. That textual data is not XML. No conformant XML tools or libraries can help you process it.
Options, most desirable first:
Have the provider fix the problem on their end. Demand well-formed XML. (Technically the phrase well-formed XML is redundant but may be useful for emphasis.)
Use a tolerant markup parser to cleanup the problem ahead of parsing as XML:
Standalone: xmlstarlet has robust recovering and repair capabilities credit: RomanPerekhrest
xmlstarlet fo -o -R -H -D bad.xml 2>/dev/null
Standalone and C/C++: HTML Tidy works with XML too. Taggle is a port of TagSoup to C++.
Python: Beautiful Soup is Python-based. See notes in the Differences between parsers section. See also answers to this question for more
suggestions for dealing with not-well-formed markup in Python,
including especially lxml's recover=True option.
See also this answer for how to use codecs.EncodedFile() to cleanup illegal characters.
Java: TagSoup and JSoup focus on HTML. FilterInputStream can be used for preprocessing cleanup.
.NET:
XmlReaderSettings.CheckCharacters can
be disabled to get past illegal XML character problems.
#jdweng notes that XmlReaderSettings.ConformanceLevel can be set to
ConformanceLevel.Fragment so that XmlReader can read XML Well-Formed Parsed Entities lacking a root element.
#jdweng also reports that XmlReader.ReadToFollowing() can sometimes
be used to work-around XML syntactical issues, but note
rule-breaking warning in #3 below.
Microsoft.Language.Xml.XMLParser is said to be “error-tolerant”.
Go: Set Decoder.Strict to false as shown in this example by #chuckx.
PHP: See DOMDocument::$recover and libxml_use_internal_errors(true). See nice example here.
Ruby: Nokogiri supports “Gentle Well-Formedness”.
R: See htmlTreeParse() for fault-tolerant markup parsing in R.
Perl: See XML::Liberal, a "super liberal XML parser that parses broken XML."
Process the data as text manually using a text editor or
programmatically using character/string functions. Doing this
programmatically can range from tricky to impossible as
what appears to be
predictable often is not -- rule breaking is rarely bound by rules.
For invalid character errors, use regex to remove/replace invalid characters:
PHP: preg_replace('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', ' ', $s);
Ruby: string.tr("^\u{0009}\u{000a}\u{000d}\u{0020}-\u{D7FF}\u{E000‌​}-\u{FFFD}", ' ')
JavaScript: inputStr.replace(/[^\x09\x0A\x0D\x20-\xFF\x85\xA0-\uD7FF\uE000-\uFDCF\uFDE0-\uFFFD]/gm, '')
For ampersands, use regex to replace matches with &: credit: blhsin, demo
&(?!(?:#\d+|#x[0-9a-f]+|\w+);)
Note that the above regular expressions won't take comments or CDATA
sections into account.
A standard XML parser will NEVER accept invalid XML, by design.
Your only option is to pre-process the input to remove the "predictably invalid" content, or wrap it in CDATA, prior to parsing it.
The accepted answer is good advice, and contains very useful links.
I'd like to add that this, and many other cases of not-wellformed and/or DTD-invalid XML can be repaired using SGML, the ISO-standardized superset of HTML and XML. In your case, what works is to declare the bogus THIS-IS-PART-OF-DESCRIPTION element as SGML empty element and then use eg. the osx program (part of the OpenSP/OpenJade SGML package) to convert it to XML. For example, if you supply the following to osx
<!DOCTYPE xml [
<!ELEMENT xml - - ANY>
<!ELEMENT description - - ANY>
<!ELEMENT THIS-IS-PART-OF-DESCRIPTION - - EMPTY>
]>
<xml>
<description>blah blah
<THIS-IS-PART-OF-DESCRIPTION>
</description>
</xml>
it will output well-formed XML for further processing with the XML tools of your choice.
Note, however, that your example snippet has another problem in that element names starting with the letters xml or XML or Xml etc. are reserved in XML, and won't be accepted by conforming XML parsers.
IMO these cases should be solved by using JSoup.
Below is a not-really answer for this specific case, but found this on the web (thanks to inuyasha82 on Coderwall). This code bit did inspire me for another similar problem while dealing with malformed XMLs, so I share it here.
Please do not edit what is below, as it is as it on the original website.
The XML format, requires to be valid a unique root element declared in the document.
So for example a valid xml is:
<root>
<element>...</element>
<element>...</element>
</root>
But if you have a document like:
<element>...</element>
<element>...</element>
<element>...</element>
<element>...</element>
This will be considered a malformed XML, so many xml parsers just throw an Exception complaining about no root element. Etc.
In this example there is a solution on how to solve that problem and succesfully parse the malformed xml above.
Basically what we will do is to add programmatically a root element.
So first of all you have to open the resource that contains your "malformed" xml (i. e. a file):
File file = new File(pathtofile);
Then open a FileInputStream:
FileInputStream fis = new FileInputStream(file);
If we try to parse this stream with any XML library at that point we will raise the malformed document Exception.
Now we create a list of InputStream objects with three lements:
A ByteIputStream element that contains the string: <root>
Our FileInputStream
A ByteInputStream with the string: </root>
So the code is:
List<InputStream> streams =
Arrays.asList(
new ByteArrayInputStream("<root>".getBytes()),
fis,
new ByteArrayInputStream("</root>".getBytes()));
Now using a SequenceInputStream, we create a container for the List created above:
InputStream cntr =
new SequenceInputStream(Collections.enumeration(str));
Now we can use any XML Parser library, on the cntr, and it will be parsed without any problem. (Checked with Stax library);

C# XML - Collapse XML properties into a list/array

I'm trying to serialize an XML file returned from a SOAP API service. The issue is that they have defined an array of objects as such:
<soap_xml>
...
<item __array="true">
<some_property>text here<some_property/>
</item>
<item>
<some_property>text here<some_property/>
</item>
...
</soap_xml>
Is there anyway to use the XmlSerializer to condense this down into an array when deserializing this XML file, or will I have to process the entire XML file manually. I'm not keen on having to process the XML manually, since it has over 100 different properties/fields, but if there is no other solution, then I'll have to use a XMLReader and write a custom serializer.
Also, asking the API provider to change the format of the returned XML is out of the question.
You'll need to define a class that follows the schema of the XML file - do you have a schema from the API provider? If so, you could use xsd.exe to generate a class file that can load it.
Alternatively, you'll need to annotate your own class with the appropriate XML attributes that follows the format.

Is there a XmlReader method similar to the XmlWriter.WriteRaw method?

XmlWriter.WriteRaw will preserve &apos; and not send an actual apostrophe. Is there a method to read in &apos; and keep it as such?
You need to encode it properly. Let's take for example the following XML:
<root>&apos;</root>
The value of the <root> node is ' no matter which XML parser you use to read this XML.
On the other hand if you have the following XML:
<root>&apos;</root>
the value of the <root> node is &apos;.
So in both cases we have properly encoded XML so that when a standard compliant parser reads it, it is able to correctly retrieve the value.
So be very careful when using the WriteRaw method when generating the XML. Since it properly encode the argument it is now your responsibility to ensure that you are passing correct data to it.

XSLT: transfer xml with the closed tags

I'm using XSLT transfer an XML to a different format XML. If there is empty data with the element, it will display as a self-closing, eg. <data />, but I want output it with the closing tag like this <data></data>.
If I change the output method from "xml" to "html" then I can get the <data></data>, but I will lose the <?xml version="1.0" encoding="UTF-8"?> on the top of the document. Is this the correct way of doing this?
Many thanks.
Daoming
If you want this because you think that self closing tags are ugly, then get over it.
If you want to pass the output to some non-conformant XML Parser that is under control, then use a better parser, or fix the one you are using.
If it is out of your control, and you must send it to an inadequate XML Parser, then do you really need the prolog? If not, then html output method is fine.
If you do need the XML prolog, then you could use the html output method, and prepend the prolog after transformation, but before sending it to the deficient parser.
Alternatively, you could output it as XML with self-closing tags, and preprocess before sending it to your deficient parser with some kind of custom serialisation, using the DOM. If it can't handle self-closing tags, then I'm sure that isn't the only way in which it fails to parse XML. You might need to do something about namespaces, for example.
You could try adding an empty text node to any empty elements that you are outputting. That might do the trick.
Self-closed and explicitly closed elements are exactly the same thing in any regard whatsoever.
Only if somewhere along your processing chain there is a tool that is not XML aware (code that does XML processing with regex, for example), it might make a difference. At which point you should think about changing that part of the processing, instead of the XML generation/serialization part.

Categories

Resources