Reading Xml with XmlReader in C#

后端 未结 7 2093
南旧
南旧 2020-11-22 09:02

I\'m trying to read the following Xml document as fast as I can and let additional classes manage the reading of each sub block.


             


        
7条回答
  •  逝去的感伤
    2020-11-22 09:58

    For sub-objects, ReadSubtree() gives you an xml-reader limited to the sub-objects, but I really think that you are doing this the hard way. Unless you have very specific requirements for handling unusual / unpredicatable xml, use XmlSerializer (perhaps coupled with sgen.exe if you really want).

    XmlReader is... tricky. Contrast to:

    using System;
    using System.Collections.Generic;
    using System.Xml.Serialization;
    public class ApplicationPool {
        private readonly List accounts = new List();
        public List Accounts {get{return accounts;}}
    }
    public class Account {
        public string NameOfKin {get;set;}
        private readonly List statements = new List();
        public List StatementsAvailable {get{return statements;}}
    }
    public class Statement {}
    static class Program {
        static void Main() {
            XmlSerializer ser = new XmlSerializer(typeof(ApplicationPool));
            ser.Serialize(Console.Out, new ApplicationPool {
                Accounts = { new Account { NameOfKin = "Fred",
                    StatementsAvailable = { new Statement {}, new Statement {}}}}
            });
        }
    }
    

提交回复
热议问题