【问题标题】:Xml from url to c#xml从url到c#
【发布时间】:2021-07-22 17:40:50
【问题描述】:

我无法将 xml 从 url 解析为 c#。我有以下代码,它给出了一个错误: System.InvalidOperationException: 'XML 文档 (1, 17) 中存在错误:

我必须下载 xml 解析它并将数据保存到存储中,但是有好几个小时我只能编写这样的代码,我不明白在这里做什么。

程序.cs

using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Linq;


public class categories
{
    public string parentId;
    public int categoryId;

    [XmlElement("categories")]
    public List<categories> categoriess;

    public override string ToString()
    {
        return string.Format("(categories) name: {0}, age: {1}\n", parentId, categoryId) +
            string.Join("", categoriess.Select(x => x.ToString()));
    }
}

public class Test
{                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
    public static void Main()
    {
        String URLString = "https://drive.google.com/file/d/1sSR9kWifwjIP5qFWcyxGCxN0-MoEd_oo/view";
        XmlTextReader reader = new XmlTextReader(URLString);
        var result = (categories)new XmlSerializer(typeof(categories)).Deserialize(reader);

            Console.WriteLine(result.ToString());
        }
}

Xml

<?xml version="1.0" encoding="windows-1251"?>
<!DOCTYPE yml_catalog SYSTEM "shops.dtd">
<yml_catalog date="2021-07-22 12:07">
<shop>
    <name>Radio-Liga</name>
    <company>Magazine</company>
    <url>http://www.radio-liga.ru/</url>
    
    <currencies>
        <currency id="RUR"/>
    </currencies>
    
    <categories>
        <category id="544">CI-BI AutoStations</category>
        <category id="499" parentId="544">ALAN</category>
        <category id="513" parentId="544">MEGAJET</category>
        <category id="514" parentId="544">YOSAN</category>
        <category id="571" parentId="544">PRESIDENT</category>
        <category id="559" parentId="544">ALBRECHT</category>
        <category id="565" parentId="544">OPTIM</category>
        <category id="727" parentId="544">VECTOR</category>
        <category id="498" parentId="545">MIDLAND</category>
        <category id="528" parentId="545">ROGER</category>
        <category id="603" parentId="545">BAOFENG</category>>
        <category id="604" parentId="545">LIRA</category>
        <category id="626" parentId="545">TYT</category>
        <category id="680" parentId="545">TURBOSKY</category>
        <category id="681" parentId="545">COMRADE </category>

【问题讨论】:

标签: c# xml


【解决方案1】:

您还没有在此处发布整个 XML,所以很难说。此外,错误消息看起来不完整。

这可能是 encoding="windows-1251" 的问题。一些解析器无法识别这种编码。

这可能是 DTD 的问题(对 shop.dtd 的引用)。

当您无法解决 XML 解析错误时,最简单的解决方案有时是将文档粘贴到不同的 XML 编辑器中 - 其中一些通常比其他编辑器具有更好的诊断能力。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-26
    • 1970-01-01
    • 2021-09-14
    • 2012-04-06
    • 2018-03-16
    • 2021-05-03
    • 2011-05-02
    • 2012-12-15
    相关资源
    最近更新 更多