문제

I am trying to read below text from XML file. but facing problem as its not reading it.

<link rel="self" type="application/json" href="https://api.demo.com/1/2/search?client_id=7f9d55eaaa844b48bb3cd98040f84382&DD=5000&BB=40.7142&AA=-74.0064"/>

Giving me error for reading '=', '&' special characters.

XML Exception was unhandled'"' is an unexpected token. The expected token is ';'. Line 9, position 170.

As these characters are auto generated from source. what can I do to avoid this issue.

Below is the code snippet I am using.

Dictionary<string, object> idict = new System.Collections.Generic.Dictionary<string, object>();  
        using (XmlReader reader = XmlReader.Create(strXMLPath))
        {
            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element && reader.LocalName == "data")
                {
                    reader.MoveToAttribute("name");
                    string key = reader.Value;
                    reader.MoveToContent();
                    object value = reader.ReadElementContentAsObject();
                    idict.Add(key, value);
                }
            }
            reader.Close();
        }
도움이 되었습니까?

해결책

The snipet of your XML is invalid (character '=' is grammatically unexpected). I seggest (if you can), to use a CData in which your href will be put. So your XML will be like :

<link rel="self" type="application/json">
    <href><![CDATA[https://api.demo.com/1/2/search?client_id=7f9d55eaaa844b48bb3cd98040f84382&DD=5000&BB=40.7142&AA=-74.0064]]></href>
</link>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top