从解析的数据中编写XML

时间:2014-02-16 21:28:21

标签: c# html-agility-pack

我编写了一段代码来解析网站顶部导航中的链接。

private string url = "http://www.blah.com/";
    private HtmlWeb web;
    private HtmlDocument doc;

    private string topNavName = "";
    private string topNavUrl = "";

    public void Setup()
    {
        try
        {
            web = new HtmlWeb();
            doc = web.Load(url);
            web.AutoDetectEncoding = true;
            TopCats();
        }
        catch (Exception e)
        {
            Console.WriteLine("There has been an issue loading url {0}", e);
        }
    }

    private List<Catalogue> TopCats()
    {
      List<Catalogue> GetTop = new List<Catalogue>();
        try
        {
            HtmlNodeCollection TopNode = doc.DocumentNode.SelectNodes("//*[@id='TopTabs1_tabs']/li/span/a");
            if (TopNode != null)
            {
                foreach (HtmlNode Topitem in TopNode)
                {
                    topNavName = Topitem.InnerText;
                    topNavUrl = url + Topitem.Attributes["href"].Value;

                    Catalogue xmltopcat = new Catalogue();
                    xmltopcat.indentifier = "here";
                    xmltopcat.name = topNavName;
                    xmltopcat.url = topNavUrl;
                    xmltopcat.description = "";

                    Console.WriteLine("Category >> {0}",topNavName);
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("There has been an issue Top Nav {0}", e);
        }
        return GetTop;
    }
}

我遇到的问题是我不确定如何使for each循环中的每个解析数据填充XML元素。对于XML上的映射,我创建了一个新类:

class Catalogue
{
    [XmlElement("Category identifier")]
    public string indentifier
    { get; set; }

    [XmlElement("name")]
    public string name
    { get; set; }

    [XmlElement("url")]
    public string url
    { get; set; }

    [XmlElement("description")]
    public string description
    { get; set; }
}

我真的不确定要创建一个XML文档 - 我尝试了一些事情,而且我真的不确定我在做什么。我还在学习C#,这是我第一次使用XML。

1 个答案:

答案 0 :(得分:1)

您可以使用LINQ to XML。首先将您的所有Catalogues存储到List

Catalogue xmltopcat = new Catalogue();
xmltopcat.indentifier = "here";
xmltopcat.name = topNavName;
xmltopcat.url = topNavUrl;
xmltopcat.description = "";
GetTop.Add(xmltopcat); // <-- add current catalogue to the list

然后调用TopCats方法获取列表并创建XML文件:

var list = TopCats();

XElement xDoc  = new XElement("Catalogues", 
                              list.Select(c => new XElement("Catalogue",
                              new XElement("indentifier",c.indentifier)
                              new XElement("name",c.name)
                              new XElement("url",c.url)
                              new XElement("description",c.description)));
xDoc.Save("savepath");

或者您可以使用XmlSerializer

FileStream fs = new FileStream("records.xml",FileMode.OpenOrCreate,FileAccess.Write);
XmlSerializer serializer = new XmlSerializer(typeof(List<Catalogue>),new XmlRootAttribute("Catalogues"));
serializer.Serialize(fs,list);