0

Trying to load a string value into a XML document in C#. This code has been working fine up until recently. I cannot figure out why it is now failing...

strJournalReturn = ServiceConnect.PostInventoryJournal(userName, password, 
type, effectiveDate, fromWarehouse, toWarehouse, department, project, asset, 
workOrder, jobNumber, reason, lTransactions);

    if (!strJournalReturn.Contains("INTERNALERROR"))
        {
            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.LoadXml(strJournalReturn);
        }

the strJournalReturn value is...

"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<JOURNAL-
SERVICE>\n<ERROR>1</ERROR>\n<ERRORMESSAGES>\n<ERRORMESSAGE>DEPARTMENT is 
required for direct issues & reserves with 
backorders</ERRORMESSAGE>\n</ERRORMESSAGES>\n</JOURNAL-SERVICE>"

This is throwing an exception "NotSupportedException". Any thoughts/ideas would be appreciated.

I apologize in advance if the detail on this post is not enough as this is my first time posting here.

stackuser83
  • 2,012
  • 1
  • 24
  • 41
gdoyon
  • 23
  • 3
  • @RyanWilson I don't think this is about the error in the xml, but an error attempting to load the xml into a `XmlDocument`. – juharr Feb 05 '18 at 17:39
  • That is the error I am expecting to get back in this scenario. However, when trying to load that XML string into the XmlDocument, it blows up with "NotSupportedException". I assume the XML is not formatted correctly but I am not seeing any problems. – gdoyon Feb 05 '18 at 17:41
  • 4
    The string content isn't xml encoded correctly. Eg & – StuartLC Feb 05 '18 at 17:42
  • You might want to consider switching to the Linq-to-Xml DOM instead. – juharr Feb 05 '18 at 17:47
  • It's the ampersand, yes. Make sure whatever produces this does its xml encoding correctly. – Nyerguds Feb 05 '18 at 17:48
  • Yes it was indeed the ampersand. I was focused on the tags themselves and never thought to even look at the message coming back. Thanks! – gdoyon Feb 05 '18 at 17:53
  • 1
    FYI, Having a method with so many parameters is considered an anti-pattern. *Code Complete, 2nd edition (McConnell)*, page 178: "Limit the number of a routine's parameters to about seven. Seven is the magic number for people's comprehension. Research has found that people generally cannot keep track of more than seven chunks of information at a time... If you find yourself consistently passing more than a few arguments, the coupling between your routines is too tight. Design the routine or group of routines to reduce coupling." –  Feb 05 '18 at 18:00

1 Answers1

0

The string which you want to parse has invalid character like ampersand.(&)

DEPARTMENT is required for direct issues & reserves with backorders

Normally, the xml should be encoded properly. Using Replace would be an option before trying to parse it.

var str = "<JOURNAL-SERVICE><ERROR>1</ERROR><ERRORMESSAGES>" +
            "<ERRORMESSAGE>DEPARTMENT is required for direct issues & reserves with backorders</ERRORMESSAGE>" +
            "</ERRORMESSAGES></JOURNAL-SERVICE>";
str = str.Replace("&", "&amp;");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(str);

But, be sure about that the another non-encoded characters might be contained in the xml.

lucky
  • 12,734
  • 4
  • 24
  • 46