parsing XML with ampersand

后端 未结 9 1385
孤城傲影
孤城傲影 2020-11-30 12:21

I have a string which contains XML, I just want to parse it into Xelement, but it has an ampersand. I still have a problem parseing it with HtmlDecode. Any suggestions?

相关标签:
9条回答
  • 2020-11-30 12:57

    If your string is not valid XML, it will not parse. If it contains an ampersand on its own, it's not valid XML. Contrary to HTML, XML is very strict.

    0 讨论(0)
  • 2020-11-30 13:07

    This is the simplest and best approach. Works with all characters and allows to parse XML for any web service call i.e. SharePoint ASMX.

    public string XmlEscape(string unescaped)
            {
                XmlDocument doc = new XmlDocument();
                var node = doc.CreateElement("root");
                node.InnerText = unescaped;
                return node.InnerXml;
            }
    
    0 讨论(0)
  • 2020-11-30 13:11

    The ampersant makes the XML invalid. This cannot be fixed by a stylesheet so you need to write code with some other tool or code in VB/C#/PHP/Delphi/Lisp/Etc. to remove it or to translate it to &.

    0 讨论(0)
提交回复
热议问题