【问题标题】:xml unable to read URLxml 无法读取 URL
【发布时间】:2014-01-27 11:09:25
【问题描述】:

我正在尝试从 XML 文件中读取以下文本。但面临问题,因为它没有阅读它。

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

读取 '='、'&' 特殊字符时出现错误。

XML Exception was unhandled'"' 是一个意外的标记。预期的标记是 ';'。第 9 行,位置 170。

因为这些字符是从源自动生成的。我该怎么做才能避免这个问题。

下面是我正在使用的代码 sn-p。

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

【问题讨论】:

    标签: c# xml xmlreader


    【解决方案1】:

    XML 的代码段无效 (character '=' is grammatically unexpected)。我建议(如果可以的话)使用CData,其中将放置您的href。所以你的XML 会是这样的:

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

    【讨论】:

    • 您的解决方案运行良好。但是这个 xml 来自提供者。我无法更新它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-17
    相关资源
    最近更新 更多