记录一种读取XML的方法

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace ConsoleApplication_readxml
{
    class Program
    {
        static void Main(string[] args)
        {
            string xmlString = @"
                        <feed>
                            <id>1234</id>
                            <entry>
                                <eid>1</eid>
                                <content>c1</content>
                            </entry>
                            <entry>
                                <eid>2</eid>
                                <content>c2</content>
                            </entry>
                        </feed>
                                ";

            byte[] xmlBytes = Encoding.UTF8.GetBytes(xmlString);
            MemoryStream stream = new MemoryStream(xmlBytes);

            XmlDocument document = new XmlDocument();
            document.Load(stream);

            var entryNodes = document.GetElementsByTagName("eid");
            var contentNodes = document.GetElementsByTagName("content");

            for (int i = 0; i < entryNodes.Count; i++)
            {
                Console.WriteLine("entry = {0}, content = {1}", entryNodes[i].InnerText, contentNodes[i].InnerText);
            }

            Console.ReadLine();
        }
    }
}

 

posted @ 2014-08-08 23:39  chunyih  阅读(158)  评论(0编辑  收藏  举报