C#XmlDocument.LoadXml(字符串)失败(NotSupportedException)

时间:2018-02-05 17:33:51

标签: c# xml

尝试在C#中将字符串值加载到XML文档中。直到最近,这段代码一直很好。我无法弄清楚为什么它现在失败了......

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);
        }

strJournalReturn值是......

"<?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>"

这会引发异常“NotSupportedException”。任何想法/想法都将不胜感激。

如果这篇文章的详细信息不够,我提前道歉,因为这是我第一次在这里发帖。

1 个答案:

答案 0 :(得分:0)

您要解析的字符串包含无符号字符,如&符号。(DEPARTMENT is required for direct issues & reserves with backorders

Replace

通常,xml应该正确编码。在尝试解析之前,使用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); 是一个选项。

{ }

但是,请确保xml中可能包含另一个非编码字符。