XML parsing throwing an exception for '&' and '<' characters [duplicate] - c#

This question already has answers here:
How to parse invalid (bad / not well-formed) XML?
(4 answers)
Closed 3 years ago.
Getting exception while parsing the XML if it contains '&' and '<' characters. I have read somewhere that having these characters in XML means that XML is not valid, but I'm receiving it from third party where I can't reformat it.
Below is my code of XML parsing using XDocument:
string data = profile.Content.ReadAsStringAsync().Result; //Read input
XDocument doc = new XDocument();
if (data != "")
{
string rawHtml = WebUtility.HtmlDecode(data);
doc = XDocument.Parse(rawHtml); //Parse input into XDocument
}
Here, data contains actual XML input and not XML filepath.
Please suggest me how to handle these special characters.

This data is not XML.
Check what you agreed with the third party.
If the contract was to exchange data in XML, then they are failing to satisfy the contract and you should deal with it the way you would deal with any other faulty goods from a supplier: return it and ask for your money back.
If the agreement didn't specify that they would send you XML, then you shouldn't be trying to parse it with an XML parser.

Related

Set XML as Value of XElement [duplicate]

This question already has answers here:
How to avoid System.Xml.Linq.XElement escaping HTML content?
(4 answers)
Closed 7 years ago.
My method receives a XML string as the input and I need to put this XML string into XML envelope using XElement:
input: <hello>Hello!</hello>
expected result: <envelope><hello>Hello!</hello></envelope>
The problem is that this code:
string xmlHello = "<hello>Hello!</hello>";
XElement xelem = new XElement("envelope", xmlHello);
escapes all <> and so the result is:
<envelope><hello>Hello!</hello></envelope>
Is there any way to disable this behaviour of the XElement constructor to be able to accept XML as the value? The input string can be really huge, so I would like to avoid parsing it.
As mentioned in the comments, this can't be done directly as the API has no way of knowing your text is actually well formed XML unless you pass it something it knows is an XML element.
So what you need to do is parse your XML first:
string xmlHello = "<hello>Hello!</hello>";
var hello = XElement.Parse(xmlHello);
var envelope = new XElement("envelope", hello);
Resulting in:
<envelope>
<hello>Hello!</hello>
</envelope>

How to convert string xml to json in c# and send over SOAP in C# [duplicate]

This question already has answers here:
How to convert JSON to XML or XML to JSON?
(14 answers)
Closed 5 years ago.
How to convert xml data into JSON format. I want to send it over the SOAP, too.
Could you provide some example?
You can use JsonConvert class to convert.
here is the code
// To convert an XML node contained in string xml into a JSON string
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
string jsonText = JsonConvert.SerializeXmlNode(doc);
// To convert JSON text contained in string json into an XML node
XmlDocument doc = JsonConvert.DeserializeXmlNode(json);
The above code is from here How to convert JSON to XML or XML to JSON?
For sending data through soap Client to send SOAP request and received response

Xelement converting special characters to strings [duplicate]

This question already has an answer here:
How to stop XDocument.Save writing escape chars
(1 answer)
Closed 2 years ago.
The below XElement converts the special character "&" to "&".
XElement newElement = new XElement("testting", "wow&testvalue");
I want it to be the "&" not &.
I want it to be the "&" not &.
Then it would be invalid XML. Why do you want invalid XML?
LINQ to XML is expressing the text you've requested in valid XML. That's what it's meant to do. If you ask for the text of the element later (through this or any other decent XML API) you'll get back wow&testvalue.
As Tim says, you could use a CDATA section:
var element = new XElement("testing", new XCData("wow&testvalue"));
But you can't tell LINQ to XML not to escape what it needs to escape...

How can html be parsed as XML when containing '...&body='? [duplicate]

This question already has answers here:
What is the best way to parse html in C#? [closed]
(15 answers)
Closed 8 years ago.
I have html file that is a well-formed xml document (tags are paired), but contains anchor like the one below:
link
Xml parser invoked by XDocument.Load throws XmlException that says:
Additional information: '=' is an unexpected token. The expected token is ';'.
How can I instruct parser that I '&body' is not an entity? Do I must escape '&' character?
Not all HTML is going to be valid XML so you shouldn't try to parse it as such (although, in this case, it looks like you have some un-escpaped strings in the document that should probably get taken care of).
Instead, you should use something like the HTMLAgilityPack to parse your HTML and work with the document that way.

Preserve the tab spacing and white spaces in attributes of XML [duplicate]

This question already has answers here:
opening/saving xml while preserving newline between node's attributes
(2 answers)
Closed 9 years ago.
I have a very basic knowledge on C# and XML. I am trying to load an XML document using XMLDocument and then edit the values of some attributes of the XML and finally save the document with the changes. The problem I am facing is, I cannot get the same formatting that i have in my original document after editing and saving it.
For example the original XML document looks like below,
<M A="XML">
<N A="XMLLINE1" B="1" C="2" D="3" D="4" />
<N A="XMLLINE2" B="5" C="6" D="7" D="8" />
</M>
After editing the value of B ="1" to B="10", I save the document. Now the spacing between the attributes A,B,C and D are not staying the same. Is there any way to preserve those spaces as they are and just edit the values and save the document?
The requirement for this document is to have those spaces as they are in original document.
Thank
You can't do this. If you want to preserve the space, better you can write your own class to generate XML by using StringBuilder or stream or you can use XMLWriter(http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter_members(v=vs.71).aspx) to manually format your document.

Categories

Resources