I am getting the following exception when I am trying to deserialize the xml document. Xml document has a tag as url in which google search link may present. Google search link contains '=' which is not accepted in the xml document while deserializing it. I am getting the xml from server. So I cannot do anything with the string that is present in the url tag. I have to do something on my client part. How can I overcome this problem?
<?xml version="1.0" encoding="UTF-8"?>
<response>
<status>
<code>000</code>
<message>Successfully completed</message>
</status>
<reports>
<report>
<id>9973</id>
<url>http://www.google.com/search?q=guns&client=safari&safe=active</url>
</report>
</reports>
</response>
Exception :
An exception of type 'System.InvalidOperationException' occurred in System.Xml.XmlSerializer.dll but was not handled in user code
Innerexception:
{"'=' is an unexpected token. The expected token is ';'. Line 136, position 53."}
Your XML is invalid. The URL is breaking the XML standard. Specifically you should escape the &: &.
This is the valid XML:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<status>
<code>000</code>
<message>Successfully completed</message>
</status>
<reports>
<report>
<id>9973</id>
<url>http://www.google.com/search?q=guns&client=safari&safe=active</url>
</report>
</reports>
</response>
Check your XML export function to make sure it escapes the URL properly.
Related
I wrote several lines of code but still can't get over this:
I need to load many xml docs from web library. I don't know how many documents there are so I wonder which loop should I use while loading:
XDocument doc = XDocument.Load("http://" + i);
where -i is identifiers number.
I tried loading until i get document without meaningful content (thought it is the end, the rest are empty), but problem is that there is several Xdocs that are empty in the middle of library.
XML with content looks like
<?xml version="1.0" encoding="utf-8"?>
<OP xmlns="" xmlns:xsi="" xsi:schemaLocation="">
<request verb="GR" identifier="53" metadataPrefix="p"></request>
<GR>
<header>
<identifier>53,number of doc...used for counting</identifier>
</header>
<metadata>
<P xmlns="" xsi:schemaLocation="">
<TITLE>title</TITLE>
<CERTIFICATE NAME="different names">
</CERTIFICATE>
<YEAR>
<DATE>2012-10-18T00:00:00Z</DATE>
</YEAR>
<MINIATURE>
<COPY>
<CNAME>Copy name<CNAME>
<FORMAT>obj/max/dxf/3ds/...</FORMAT>
</COPY>
</MINIATURE>
</metadata>
</GR>
</OP>
XML without content
<?xml version="1.0" encoding="utf-8"?>
<OP xmlns="" xmlns:xsi="" xsi:schemaLocation="">
<request verb="GR" identifier="53" metadataPrefix="p"></request>
Furthermore, I need to do some counting like:
Tot.no. of doc,
No. of docs per certificate <CERTIFICATE>
No. of docs for each year <YEAR><DATE>
No of docs for each format <MINIATURE><COPY><FORMAT>
and my output should look like:
<?xml version="1.0" encoding="UTF-8" ?>
<Statistic>
<DocSum>21220</DocSum>
<Certificates>
<Certificate id=”certificateName”>17098</Certificate>
…
<Certificates>
<Years>
<Year year=”2014”>23</Year>
…
</Years>
<Miniature>
<Format post=”obj”>11723</Format>
…
</Miniature>
</Statistic>
If you could give me some help, hints or tips how to deal with it.
The posted answer by smink to the following thread should get you on the right path.
C# HttpWebRequest command to get directory listing
One of the easiest ways to get a list of the files of a web directory without knowing exactly how many there are or their filenames is by parsing the html of the directory and pulling out the tags.
You can then iterate through these tags and filter them out for the files by extensions that you need. I can provide a more in-depth example if necessary.
I'm having hard-time fixing this little problem of ampersand (&) in the url... I'm serializing XML as shown below...
var ser = new XmlSerializer(typeof(response));
using (var reader = XmlReader.Create(url))
{
response employeeResults = (response)ser.Deserialize(reader); //<<error when i pass with ampersand
}
the above codes works fine if there is no & in the url otherwise it throws me an error (see below)
i have no problem serializing this url:
http://api.host.com/api/employees.xml/?&search=john
I'm having problem with this url:
http://api.host.com/api/employees.xml/?&max=20&page=10
The error i'm getting is:
`There is an error in XML document (1, 389).`
PS: I did tried passing & and also tried with & or #026 or & - no luck.
This XML isn't well-formed:
<?xml version="1.0"?>
<response xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Api">
<meta>
<status>200</status>
<message />
<resultSet>
<Checked>true</Checked>
</resultSet>
<pagination>
<count>1</count>
<page>1</page>
<max>1</max>
<curUri>http://api.host.com/employee.xml/?&max=5</curUri>
<prevUri i:nil="true"/>
<nextUri>http://api.host.com/employee.xml/?&max=5&page=2</nextUri>
</pagination>
</meta>
<results i:type="ArrayOfemployeeItem">
<empItem>
<Id>CTR3242</Id>
<name>john</name>
......
</empItem>
</results>
</response>
You must escape & character or put entire string in CDATA, e.g.:
<?xml version="1.0"?>
<response xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Api">
<meta>
<status>200</status>
<message />
<resultSet>
<Checked>true</Checked>
</resultSet>
<pagination>
<count>1</count>
<page>1</page>
<max>1</max>
<curUri><![CDATA[http://api.host.com/employee.xml/?&max=5]]></curUri>
<prevUri i:nil="true"/>
<nextUri><![CDATA[http://api.host.com/employee.xml/?&max=5&page=2]]></nextUri>
</pagination>
</meta>
<results i:type="ArrayOfemployeeItem">
<empItem>
<Id>CTR3242</Id>
<name>john</name>
......
</empItem>
</results>
</response>
If you are dealing with some third-party system and not able to get proper XML response, you have to do some pre-processing.
Maybe the simplest way is just replace all & with & using string.Replace method.
Or use this regex &(?!amp;) to replace all & excluding correct ones like &.
Have you tried to wrap the Attribute with <![CDATA[yourAttribute]]> ?
& is not allowed in xml
deserialize-xml-with-ampersand-using-xmlserializer
I added a reference to a Web Service in my project. I'm using the generated code to call the methods of the Web Service. One of the methods returns the following structure:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:typens="urn:AllegroWebApi" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<typens:doGetCatsDataResponse>
<cats-list xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="typens:CatInfoType[24051]">
<item xsi:type="typens:CatInfoType">
<cat-id xsi:type="xsd:int">26013</cat-id>
<cat-name xsi:type="xsd:string">Antyki i Sztuka</cat-name>
<cat-parent xsi:type="xsd:int">0</cat-parent>
<cat-position xsi:type="xsd:int">0</cat-position>
<cat-is-product-catalogue-enabled xsi:type="xsd:int">0</cat-is-product-catalogue-enabled>
</item>
....
</item>
<item xsi:type="typens:CatInfoType">
<cat-id xsi:type="xsd:int">124895</cat-id>
<cat-name xsi:type="xsd:string">Pozostałe</cat-name>
<cat-parent xsi:type="xsd:int">124883</cat-parent>
<cat-position xsi:type="xsd:int">5</cat-position>
<cat-is-product-catalogue-enabled xsi:type="xsd:int">0</cat-is-product-catalogue-enabled>
</item>
<item xsi:type="typens:CatInfoType">
<cat-id xsi:type="xsd:int">124894</cat-id>
<cat-name xsi:type="xsd:string">Teleskopy</cat-name>
<cat-parent xsi:type="xsd:int">124883</cat-parent>
<cat-position xsi:type="xsd:int">6</cat-position>
<cat-is-product-catalogue-enabled xsi:type="xsd:int">0</cat-is-product-catalogue-enabled>
</item> // Line 168361 <--------------- HERE
</cats-list>
<ver-key xsi:type="xsd:long">91632766</ver-key>
<ver-str xsi:type="xsd:string">1.1.47</ver-str>
</typens:doGetCatsDataResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
The generated code that is responsible for deserializing it throws the following exception:
An unhandled exception of type 'System.InvalidOperationException'
occurred in System.Xml.dll Additional information: There is an error
in XML document (168361, 13).
I marked the line that is mentioned in the exception in the code sample. I guess it has something to do with the fact that it is the closing tag of the last item object. I have no idea why it is failing. The XML is well formated and does not seem to include any unsupported characters.
Just a guess, but check your data, could be one of the fields has a '<' or '>' throwing off the formatting of the xml document.
I see you have two closing item tags in a row. Granted, you may be excluding some of the XML file. Still, two closing tags in a row would certainly cause that error.
I currently try updating product data with the Amazon MWS and the Feeds API. My problem: Updating the Inventory and setting a new quantity for my products resolves in errors like this:
The XML you submitted is ill-formed at the Amazon Envelope XML level
at (or near) line X, column Y.
On the other hand, I export nearly the same XML to update the prices. That works just fine...
Here is an example of the XML that i upload to the Feeds API to update the quantity:
<?xml version="1.0" encoding="utf-8"?>
<AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" noNamespaceSchemaLocation="amznenvelope.xsd">
<Header>
<DocumentVersion>1.01</DocumentVersion>
<MerchantIdentifier>{SellerID}</MerchantIdentifier>
</Header>
<MessageType>Inventory</MessageType>
<Message>
<MessageID>1</MessageID>
<Inventory>
<SKU>ArtNoXX</SKU>
<Quantity>10</Quantity>
</Inventory>
</Message>
<Message>
<MessageID>2</MessageID>
<Inventory>
<SKU>ArtNoXY</SKU>
<Quantity>23</Quantity>
</Inventory>
</Message>
</AmazonEnvelope>
P.S.: I'm using C# and a XMLDocument to create the XML File...
Edit: The Error is shown multiple times. Only the first and the last 3 lines don't appear in the error log.
Example:
... (or near) line 10, column 16.
That would be
<Inventory>
Regarding to the column, it should be
>
Wrong namespace in your config ?
Yours :
noNamespaceSchemaLocation="amznenvelope.xsd"
Should be:
noNamespaceSchemaLocation="amzn-envelope.xsd"
Hi currently I have a nested XMl , having the following Structure :
<?xml version="1.0" encoding="utf-8" ?>
<Response>
<Result>
<item id="something" />
<price na="something" />
<?xml version="1.0" encoding="UTF-8" ?>
<DIDL-Lite xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/" xmlns:dlna="urn:schemas-dlna-org:metadata-1-0/">
</Result>
<NumberReturned>10</NumberReturned>
<TotalMatches>10</TotalMatches>
</Response>
Any help on how to read this using Xdocument or XMLReader will be really helpfull.
Thanks,
Subhendu
XDocument and XmlReader are both XML parsers that expect a properly formed XML as input. What you have shown is not a XML file. So the first task would be to extract the nested XML and as this is not valid XML you cannot rely on any parser to do this job. You'll need to resort to string manipulation and or regular expressions.
My suggestion would be to fix the procedure generating this invalid XML in the first place. Another suggestion is to never generate a XML file manually but use an appropriate tool for this (XmlWriter, XDocument, ...)