From XmlDocument To XmlReader .Net

后端 未结 2 576
时光取名叫无心
时光取名叫无心 2021-02-07 03:47

After an advice from a user that answered to my question I\'m tring to convert my XmlDocument code to XmlReader code but I\'m having some troubles.

This is XML (generate

相关标签:
2条回答
  • 2021-02-07 04:20

    You can use

    XmlReader xmlReader = new XmlNodeReader(xmlDoc);
    

    See: http://blog.jongallant.com/2007/01/convert-xmldocument-to-xmlreader.html

    0 讨论(0)
  • 2021-02-07 04:27

    If you are doing read only operations on an xml file then you can you use XmlReader but as @Marc Gravell points out it is difficult.

    In this situation I will create a class that wraps an XPathDocument using an XmlReader. I then create an XPathNavigator to read the data. Here's an example:

    public class MyXmlReader
    {
        public MyXmlReader(string xml)
        {
            StringReader sReader = new StringReader(xml);
    
            XPathDocument xml = new XPathDocument(XmlReader.Create(sReader));
    
            xmlNav = xml.CreateNavigator();
        }
    
        private XPathNavigator xmlNav = null;
    
    
        public MyDataModel ReadMyDataModel()
        {
            MyDataModel model = new MyDataModel();
    
            model.Read(xmlNav);
    
            return model;
        }
    }
    

    As shown above, the reading of the data can then be encapsulated into an associated object model. You can see some details in my answer on this question:

    How do I manipulate an XML document one parent element at a time?

    0 讨论(0)
提交回复
热议问题