简体   繁体   English

从xml文件创建列表c#

[英]create list from xml file c#

I am getting this rss feeds and i am trying to get the data of the rss feed to a list format so that my customers can search through the data. 我正在获取此rss源,我正在尝试将rss的数据提供给列表格式,以便我的客户可以搜索数据。

this is the ONLY way that worked for this kind of xml data: 这是适用于此类xml数据的唯一方法:

xmlFile.SelectNodes("//ns:id |//ns:title | //ns:description", xmlnm);

public void MyMain(string[] args)
{
    WebRequest request = WebRequest.Create("url to xml file");
    WebResponse response = request.GetResponse();
    Stream dataStream = response.GetResponseStream();
    XmlDocument xmlDocument = new XmlDocument();
    xmlDocument.Load(dataStream);
    XmlNamespaceManager xmlnm = new XmlNamespaceManager(xmlDocument.NameTable);
    xmlnm.AddNamespace("ns", "http://www.w3.org/2005/Atom");

    ParseXML(xmlDocument, xmlnm);

    MessageBox.Show("\n---XML parsed---");
    //Console.ReadKey();
}

public void ParseXML(XmlDocument xmlFile, XmlNamespaceManager xmlnm)
{
    //this commented section WORKS FANTASTIC!!
    /* XmlNodeList nodes = xmlFile.SelectNodes("//ns:id |//ns:title | //ns:description", xmlnm);;
    foreach (XmlNode node in nodes)
    {
        MessageBox.Show(node.Name + " = " + node.InnerXml);
    }
    */

    //SO i decided to store the xml data into a list, and nothing works below. I have a created a simple RSSXML class to store the information
    XmlNodeList nodes = xmlFile.SelectNodes("//ns:id |//ns:title | //ns:description", xmlnm);
    List<RSSXML> items = new List<RSSXML>();           
    foreach (XmlNode node in nodes)
    {
        items.Select(x => new RSSXML()
        {
            id = node.InnerXml,
            title = node.InnerXml,
            description = node.InnerXml,
            //can add more fields here
        }).ToList();             

    }
    foreach (var myitems in items)
    {
        MessageBox.Show(myitems.summary.ToString());
    }
}

public class RSSXML()
{
//simple get set methods for id, title, description, etc
}

items.Select() produces a new List, you are not storing it. items.Select()生成一个新的List,你不存储它。

The basic fix: 基本修复:

XmlNodeList nodes = xmlFile.SelectNodes("//ns:id |//ns:title | //ns:description", xmlnm);
List<RSSXML> items = new List<RSSXML>();           
foreach (XmlNode node in nodes)
{
    //items.Select(x => new RSSXML()
    items.Add(new RSSXML {
    {
        id = node.InnerXml,
        title = node.InnerXml,
        description = node.InnerXml,
        //can add more fields here
    //}).ToList();             
    });
}

You could eliminate the foreach() : 你可以消除foreach():

XmlNodeList nodes = xmlFile.SelectNodes("//ns:id |//ns:title | //ns:description", xmlnm);
List<RSSXML> items = nodes.Select(n => new RSSXML()
    {
        id = n.InnerXml,
        title = n.InnerXml,
        description = n.InnerXml,
        //can add more fields here
    }).ToList(); 

And you probably want to replace .InnerXml with just .Value . 你可能想用.Value替换.InnerXml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM