質問

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