XML Name Cannot Begin with the "=" Character - c#

I've read through the similar post of % character but it seems the other issues can be solved in the header line. Are there certain characters not allowed in XML or do I need to format the document another way (In my case the "=" character is giving me trouble when trying to read in the document in C#)?
Name cannot begin with the character ' ', also similar but still fixed by header.
XElement nodes = XElement.Load(filename);
The structure of the XML is below:
<?xml version="1.0" encoding="utf-8"?>
<offer>
<data id="Salary">
<ocrstring>which is equal to $60,000.00 if working 40 hours per week</ocrstring>
<rule>.*(([+-]?\$[0-9]{1,3}(?:,?[0-9]{3})*\.[0-9]{2}))</rule>
<output></output>
</data>
<data id="Hours">
<ocrstring></ocrstring>
<rule>"(?<=working).*?(?=hours)"</rule> <!-- Error Occurring Here -->
<output>bob</output>
</data>
<data id="Location">
<ocrstring></ocrstring>
<rule>Regex2</rule>
<output>LongWindingRoad222</output>
</data>
</offer>
How can I parse the XML Document without getting the Cannot Begin with Character "=" Error

You need to use CDATA sections for all the <rule> elements.
What does <![CDATA[]]> in XML mean?
XML
<?xml version="1.0" encoding="utf-8"?>
<offer>
<data id="Salary">
<ocrstring>which is equal to $60,000.00 if working 40 hours per week</ocrstring>
<rule><![CDATA[.*(([+-]?\$[0-9]{1,3}(?:,?[0-9]{3})*\.[0-9]{2}))]]></rule>
<output></output>
</data>
<data id="Hours">
<ocrstring></ocrstring>
<rule><![CDATA["(?<=working).*?(?=hours)"]]></rule>
<!-- Error Occurring Here -->
<output>bob</output>
</data>
<data id="Location">
<ocrstring></ocrstring>
<rule>Regex2</rule>
<output>LongWindingRoad222</output>
</data>
</offer>

Related

C# Localization: 'Designer.resx' file does not autogenerate when using specific keys

Edit: I have confirmed that there are no duplicate names or invisible characters in my .resx file. Even now when I just revert to the faulty keys mentioned below in the .resx file, the Designer.resx stops autogenerating.
Following is the snippet of my .resx file:
<data
name="Account_Confirm"
xml:space="preserve">
<value>Confirm</value>
</data>
<data
name="Account_Facilities"
xml:space="preserve">
<value>Facilities</value>
</data>
<!--***********************
NOTIFICATIONS PAGE
***********************-->
<data
name="Notifications_NOTIFICATIONS"
xml:space="preserve">
<value>NOTIFICATIONS</value>
</data>
<data
name="Notifications_All"
xml:space="preserve">
<value>All</value>
</data>
<!--***********************
PAYMENT DETAILS PAGE
***********************-->
<data
name="PaymentDetails_PAYBYPHONE"
xml:space="preserve">
<value>PAY BY PHONE </value>
</data>
<data
name="PaymentDetails_PAYMENT"
xml:space="preserve">
<value>PAYMENT</value>
</data>
<data
name="PaymentDetails_Call"
xml:space="preserve">
<value>Call</value>
</data>
The Designer.resx would not just auto generate.
After trying various options and playing around for hours by commenting lines of code one by one to find the culprit line I noticed.
Changing,
name="PaymentDetails_PAYBYPHONE"
to
name="PaymentDetails_PAYBYPHONECAPS"
And Changing,
name="PaymentDetails_Call"
To
name="PaymentDetails_CallText"
Fixes the issue. After this change the Designer.resx is always auto generated with each change to the resxfile.
What could be the root cause of the issue?

There is an error in XML document

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.

Ampersand in XML url is not passed through

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 &#38 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

Ill Formed XML Code

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"

XmlException: Multiple document element was detected

I'm reading a XML file in a very simple way:
XmlTextReader reader = new XmlTextReader(dataPath);
while(reader.Read()){
switch (reader.Name){
case "language":
Debug.Log(reader.ReadString());
break;
case "file":
Debug.Log(reader.ReadString());
break;
case "arg":
Debug.Log(reader.ReadString());
break;
}
}
Where my xml is like this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<config>
<language>EN-US</language>
<file>\File\Doc\sample.txt</file>
</config>
<data>
<arg>LKR</language>
</dara>
My first problem is this:
XmlException: Multiple document element was detected. file:///C:/prj/as/sample.xml Line 7, position 2.
Mono.Xml2.XmlTextReader.ReadStartTag ()
Mono.Xml2.XmlTextReader.ReadContent ()
Mono.Xml2.XmlTextReader.Read ()
System.Xml.XmlTextReader.Read ()
LectorXML.Start () (at as/sampleXML.cs:17)
And second, my output is language and file, but NO arg. Maybe because is a different node? How can i fix this?
You can only have a single node element at the root of your document. You have a <config> and a <data>. Wrap them in a single document element:
<document>
<config>
<language>EN-US</language>
<file>\File\Doc\sample.txt</file>
</config>
<data>
<arg>LKR</arg>
</data>
</document>

Categories