0

我尝试使用 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;
            }
        }
    }

}
4

1 回答 1

0

尝试:

foreach (XBMCShow show in test.Show) {
                               ----
                                 ^

     if(show.ShowName == "Bones") {
         ...

你错过了 dot-Show 部分

于 2013-09-28T19:18:52.020 回答