我尝试使用xsd.exe为我的xml创建一个类,它似乎工作但我可以枚举类。我甚至尝试使用Xsd2Code,但这会导致此错误“有一个错误反映了类型'MySQlXML.XBMC'。”当我试着 列举清单。如果您对如何使枚举工作有任何想法,请告诉我。
这是我的xml
<?xml version="1.0" encoding="utf-8"?>
<XBMC>
<Show>
<IdShow>1</IdShow>
<TheTvDbId>248741</TheTvDbId>
<ShowName>2 Broke Girls</ShowName>
<Seasons>
<Season>
<Number>1</Number>
<Path>\\10.0.0.3\tv\2 Broke Girls\Season1\</Path>
</Season>
<Season>
<Number>3</Number>
<Path>\\10.0.0.3\tv\2 Broke Girls\Season 03\</Path>
</Season>
</Seasons>
</Show>
<Show>
<IdShow>164</IdShow>
<TheTvDbId>75682</TheTvDbId>
<ShowName>Bones</ShowName>
<Seasons>
<Season>
<Number>1</Number>
<Path>\\10.0.0.3\TV2\Bones\Season 01\</Path>
</Season>
</Seasons>
</Show>
</XBMC>
这是序列化数据的方式,但是当我尝试foreach (XBMCShow show in test)
时,我得到 foreach语句不能对'MySQlXML.XBMC'类型的变量进行操作,因为'MySQlXML.XBMC'不包含公共定义'的GetEnumerator'
TextReader reader = new StreamReader(_XMLFile);
XmlSerializer serializer = new XmlSerializer(typeof(XBMC));
test = (XBMC)serializer.Deserialize(reader);
reader.Close();
这是生成的类xsd.exe。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace MySQlXML
{
/// <remarks/>
public partial class XBMC
{
private XBMCShow[] showField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Show")]
public XBMCShow[] Show
{
get
{
return this.showField;
}
set
{
this.showField = value;
}
}
}
/// <remarks/>
public partial class XBMCShow
{
public ushort idShowField;
public string theTvDbIdField;
public string showNameField;
private XBMCShowSeason[] seasonsField;
/// <remarks/>
public ushort IdShow
{
get
{
return this.idShowField;
}
set
{
this.idShowField = value;
}
}
/// <remarks/>
public string TheTvDbId
{
get
{
return this.theTvDbIdField;
}
set
{
this.theTvDbIdField = value;
}
}
/// <remarks/>
public string ShowName
{
get
{
return this.showNameField;
}
set
{
this.showNameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("Season", IsNullable = false)]
public XBMCShowSeason[] Seasons
{
get
{
return this.seasonsField;
}
set
{
this.seasonsField = value;
}
}
}
/// <remarks/>
public partial class XBMCShowSeason
{
private byte numberField;
private string pathField;
/// <remarks/>
public byte Number
{
get
{
return this.numberField;
}
set
{
this.numberField = value;
}
}
/// <remarks/>
public string Path
{
get
{
return this.pathField;
}
set
{
this.pathField = value;
}
}
}
}
答案 0 :(得分:0)
尝试:
foreach (XBMCShow show in test.Show) {
----
^
if(show.ShowName == "Bones") {
...
你错过了点展部分