【发布时间】: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>
【问题讨论】:
-
您发布的那个xml无效。
-
这是 xml 的一部分,其余部分在这里drive.google.com/file/d/1sSR9kWifwjIP5qFWcyxGCxN0-MoEd_oo/view