Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Linq从xml文件读取到objectlist_C#_Xml_Linq - Fatal编程技术网

C# 使用Linq从xml文件读取到objectlist

C# 使用Linq从xml文件读取到objectlist,c#,xml,linq,C#,Xml,Linq,我一直试图理解如何使用linq处理xml文件 <?xml version="1.0" encoding="utf-8" standalone="yes"?> <podcasts> <podcast> <url>http://...</url> <name>blablabla</name> <frequency>20 min</frequency> <

我一直试图理解如何使用linq处理xml文件

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<podcasts>
  <podcast>
    <url>http://...</url>
    <name>blablabla</name>
    <frequency>20 min</frequency>
    <category>Drama</category>
    <episodes>
      <episode>
        <name>blablabla</name>
        <description>blablabla...</description>
      </episode>
      <episode>
        <name>blablabla</name>
        <description>blablabla...</description>
      </episode>
    </episodes>
  </podcast>
  <podcast>
    <url>http://...</url>
    <name>blablabla</name>
    <frequency>20 min</frequency>
    <category>Drama</category>
    <episodes>
      <episode>
        <name>blablabla</name>
        <description>blablabla...</description>
      </episode>
      <episode>
        <name>blablabla</name>
        <description>blablabla...</description>
      </episode>
    </episodes>
  </podcast>
</podcasts>

http://...
喋喋不休
20分钟
戏剧
喋喋不休
呜呜呜呜。。。
喋喋不休
呜呜呜呜。。。
http://...
喋喋不休
20分钟
戏剧
喋喋不休
呜呜呜呜。。。
喋喋不休
呜呜呜呜。。。
问题是我可以检索播客信息,但不能检索该播客的片段。播客列表获取播客信息,但播客对象中的情节列表不获取情节信息。当我调试它时,它说eposodelist是空的

播客和插曲课是这样的

public class Podcast
    {
        public string Url { get; set; }
        public string EpisodesQuantity { get; set; }
        public string Name { get; set; }
        public string Frequency { get; set; }
        public string Category { get; set; }
        public List<Episode> episodeList { get; set; }

    }    


public class Episode
    {
        public string Name { get; set; }
        public string Desc { get; set; }
        public string Link { get; set; }
    }
公共类播客
{
公共字符串Url{get;set;}
公共字符串代码{get;set;}
公共字符串名称{get;set;}
公共字符串频率{get;set;}
公共字符串类别{get;set;}
公共列表情节列表{get;set;}
}    
公共课插曲
{
公共字符串名称{get;set;}
公共字符串Desc{get;set;}
公共字符串链接{get;set;}
}

我真的被困在这里了,你们知道我做错了什么吗(

这应该是对的。我有类似的东西,只是根据你的需要编辑。我想你会在这方面得到指导

episodeList = (from e in p.Element("episodes").Elements("episode")
                    select new Episode
                    {
                        Name = e.Element("name").Value,
                        Desc = e.Element("description").Value
                    }).ToList()
我的代码加载了非常复杂的XML。所以你可以看到逻辑。看一看

 public static EdiFile read(XElement xmlDoc)
        {
            return new EdiFile
            {
                SPPLR_MAILBOX = xmlDoc.Element("SPPLR_MAILBOX").Value,
                MESSAGE_ID = xmlDoc.Element("MESSAGE_ID").Value,
                ASN_NO = xmlDoc.Element("ASN_NO").Value,
                MESSAGE_SEND_DATE = xmlDoc.Element("MESSAGE_SEND_DATE").Value,
                ETD = xmlDoc.Element("ETD").Value,
                ETA = xmlDoc.Element("ETA").Value,
                INVOICE_NUM = xmlDoc.Element("INVOICE_NUM").Value,
                SPPLR_CD = xmlDoc.Element("SPPLR_CD").Value,
                SPPLR_CONTACT = xmlDoc.Element("SPPLR_CONTACT").Value,
                DELIVERY_PARTY_CD = xmlDoc.Element("DELIVERY_PARTY_CD").Value,
                DELIVERY_TO_PLACE = xmlDoc.Element("DELIVERY_TO_PLACE").Value,
                DELIVERY_FROM_PLACE = xmlDoc.Element("DELIVERY_FROM_PLACE").Value,
                SHIPPING_TYPE = xmlDoc.Element("SHIPPING_TYPE").Value,
                FREIGHT_TERMS = xmlDoc.Element("FREIGHT_TERMS").Value,
                TRUCK_CONTACT = xmlDoc.Element("TRUCK_CONTACT").Value,
                TRUCK_LICENCE_NUM = xmlDoc.Element("TRUCK_LICENCE_NUM").Value,
                CREATED_BY = xmlDoc.Element("CREATED_BY").Value,
                CREATED_DATE = xmlDoc.Element("CREATED_DATE").Value,
                ATTRIBUTE01 = xmlDoc.Element("ATTRIBUTE01").Value,
                ATTRIBUTE02 = xmlDoc.Element("ATTRIBUTE02").Value,
                ATTRIBUTE03 = xmlDoc.Element("ATTRIBUTE03").Value,
                ATTRIBUTE04 = xmlDoc.Element("ATTRIBUTE04").Value,
                ATTRIBUTE05 = xmlDoc.Element("ATTRIBUTE05").Value,
                Levels0 = (from a in xmlDoc.Element("LEVELS0").Elements("LEVEL0")
                           select new Level0
                           {
                               PLT_NUM = a.Element("PLT_NUM").Value,
                               PLT_LABEL_ID = a.Element("PLT_LABEL_ID").Value,
                               BOX_COUNT = a.Element("BOX_QTY").Value,
                               PLT_WEIGHT = a.Element("PLT_WEIGTH").Value,
                               PLT_DIMENSION = a.Element("PLT_DIMENSION").Value,
                               PLT_CHEM = a.Element("PLT_CHEM").Value,
                               PLT_NOT_STACK = a.Element("PLT_NOT_STACK").Value,
                               PLT_NOTE = a.Element("PLT_NOTE").Value,
                               ATTRIBUTE01 = a.Element("ATTRIBUTE01").Value,
                               ATTRIBUTE02 = a.Element("ATTRIBUTE02").Value,
                               ATTRIBUTE03 = a.Element("ATTRIBUTE03").Value,
                               ATTRIBUTE04 = a.Element("ATTRIBUTE04").Value,
                               ATTRIBUTE05 = a.Element("ATTRIBUTE05").Value,
                               Levels1 = (from b in a.Element("LEVELS1").Elements("LEVEL1")
                                          select new Level1
                                          {
                                              BOX_NUM = b.Element("BOX_NUM").Value,
                                              BOX_LABEL_ID = b.Element("BOX_LABEL_ID").Value,
                                              Items = (from c in b.Element("ITEMS").Elements("ITEM")
                                                       select new Item
                                                       {
                                                           SPPLR_ITEM = c.Element("SPPLR_ITEM").Value,
                                                           CUST_ITEM = c.Element("CUST_ITEM").Value,
                                                           PO_NO = c.Element("PO_NO").Value,
                                                           PO_REF = c.Element("PO_REF").Value,
                                                           COUNTRY_OF_ORIGIN = c.Element("COUNTRY_OF_ORIGIN").Value,
                                                           BATCH_NO = c.Element("BATCH_NO").Value,
                                                           MLS_CLASS = c.Element("MLS_CLASS").Value,
                                                           ATTRIBUTE01 = c.Element("ATTRIBUTE01").Value,
                                                           ATTRIBUTE02 = c.Element("ATTRIBUTE02").Value,
                                                           ATTRIBUTE03 = c.Element("ATTRIBUTE03").Value,
                                                           ATTRIBUTE04 = c.Element("ATTRIBUTE04").Value,
                                                           ATTRIBUTE05 = c.Element("ATTRIBUTE05").Value,
                                                           Lots = (from d in c.Element("LOTS").Elements("LOT")
                                                                   select new Lot
                                                                   {
                                                                       LOT_NUM = d.Element("LOT_NUM").Value,
                                                                       LOT_LABEL_ID = d.Element("LOT_LABEL_ID").Value,
                                                                       LOT_NOTE = d.Element("LOT_NOTE").Value,
                                                                       LOT_EXP_DATE = d.Element("LOT_EXP_DATE").Value,
                                                                       QTY = d.Element("QTY").Value,
                                                                       UOM = d.Element("UOM").Value,
                                                                       ATTRIBUTE01 = d.Element("ATTRIBUTE01").Value,
                                                                       ATTRIBUTE02 = d.Element("ATTRIBUTE02").Value,
                                                                       ATTRIBUTE03 = d.Element("ATTRIBUTE03").Value,
                                                                       ATTRIBUTE04 = d.Element("ATTRIBUTE04").Value,
                                                                       ATTRIBUTE05 = d.Element("ATTRIBUTE05").Value
                                                                   }).ToList()
                                                       }).ToList()
                                          }).ToList()
                           }).ToList()
            };
        }
如果你是古玩,你可以在我的例子中清楚地看到它是如何工作的。

使用这个

episodeList = p.Element("episodes").Elements("episode")
    .Select(e => new Episode
    {
        Name = e.Element("name").Value,
        Desc = e.Element("description").Value
    }).ToList()

请注意,这里有一个输入错误
e.Element(“desription”)
(应该是“description”)

为什么不直接反序列化xml?
新的XmlSerializer(typeof(Podcast[])。反序列化(xmlReader);
@Obama先生你为什么要从问题中删除代码?@Obama先生我是想帮你。如果你删除代码,人们可能会开始对你的问题投反对票。他们会认为你根本没有努力解决你的问题。你为什么需要从..中删除
这里?它不会起作用,因为没有名为 描述您的代码可能会抛出一个
NullReferenceException
-它应该是
Desc=e.Element(“描述”).Value
,而不是
Desc=e.Element(“描述”).Value
@RuiJarimba ye,很抱歉我看到了,我没有注意到描述的打字错误,我只是复制了他的部分并编辑了它,我的错。@RomanMarusyk和from…in,我喜欢使用它,因为代码清除。你可以更好地在结构中定向(对我来说)