【问题标题】:Reading and writing an existing xml file c# 3.0 / .net3.5读取和写入现有 xml 文件 c# 3.0 / .net3.5
【发布时间】:2009-09-23 07:54:59
【问题描述】:

我有一个想要阅读的 xml 文件。我该怎么做?我不会在运行时加载整个 xml 文件

(XmlDocument _xd = XmlDocument.Load(path)) 

我想和Readers一起做,但我用它做不到。

同时,我想将节点添加到这个带有 writer 的 xml 文件中。这些如何与 XDocument 或 c# 3.5 一起使用。

亲切的问候。

【问题讨论】:

    标签: c# xml .net-3.5


    【解决方案1】:

    我认为this is useful。

    这是一个例子:

    using System;
    using System.Xml;
    namespace ReadXml1
    {
        class Class1
        {
            static void Main(string[] args)
            {
                // Create an isntance of XmlTextReader and call Read method to read the file
                XmlTextReader textReader = new XmlTextReader("C:\\books.xml");
                textReader.Read();
                // If the node has value
                while (textReader.Read())
                {
                    // Move to fist element
                    textReader.MoveToElement();
                    Console.WriteLine("XmlTextReader Properties Test");
                    Console.WriteLine("===================");
                    // Read this element's properties and display them on console
                    Console.WriteLine("Name:" + textReader.Name);
                    Console.WriteLine("Base URI:" + textReader.BaseURI);
                    Console.WriteLine("Local Name:" + textReader.LocalName);
                    Console.WriteLine("Attribute Count:" + textReader.AttributeCount.ToString());
                    Console.WriteLine("Depth:" + textReader.Depth.ToString());
                    Console.WriteLine("Line Number:" + textReader.LineNumber.ToString());
                    Console.WriteLine("Node Type:" + textReader.NodeType.ToString());
                    Console.WriteLine("Attribute Count:" + textReader.Value.ToString());
                }
            }
        }
    }
    

    下面是XMLWriters 的示例:

    使用 System.Xml;

    class Program
    {
        class Employee
        {
            int _id;
            string _firstName;
            string _lastName;
            int _salary;
            public Employee(int id, string firstName, string lastName, int salary)
            {
                this._id = id;
                this._firstName = firstName;
                this._lastName = lastName;
                this._salary = salary;
            }
            public int Id { get { return _id; } }
            public string FirstName { get { return _firstName; } }
            public string LastName { get { return _lastName; } }
            public int Salary { get { return _salary; } }
        }
    
        static void Main()
        {
            Employee[] employees = new Employee[4];
            employees[0] = new Employee(1, "David", "Smith", 10000);
            employees[1] = new Employee(3, "Mark", "Drinkwater", 30000);
            employees[2] = new Employee(4, "Norah", "Miller", 20000);
            employees[3] = new Employee(12, "Cecil", "Walker", 120000);
    
            using (XmlWriter writer = XmlWriter.Create("employees.xml"))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement("Employees");
    
                foreach (Employee employee in employees)
                {
                    writer.WriteStartElement("Employee");
    
                    writer.WriteElementString("ID", employee.Id.ToString());
                    writer.WriteElementString("FirstName", employee.FirstName);
                    writer.WriteElementString("LastName", employee.LastName);
                    writer.WriteElementString("Salary", employee.Salary.ToString());
    
                    writer.WriteEndElement();
                }
    
                writer.WriteEndElement();
                writer.WriteEndDocument();
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      这里是一个使用XDocument加载xml文件,添加子节点并保存以供使用的示例:

      // Load XML file : 
      XDocument xdoc = XDocument.Load(path);
      
      // Parse XML : 
      //XDocument xdoc = XDocument.Parse("<YourRootElement><ChildElement>Child 1</ChildElement></YourRootElement>");
      
      
      // Add Child Node to loaded xml : 
      xdoc.Element("YourRootElement").Add(
           new XElement("ChildElement", "Child 2"));
      
      // Save XML to file : 
      xdoc.Save(path);
      

      编辑:使用XDocument.Parse method从内存中加载XML。

      【讨论】:

        【解决方案3】:

        您没有考虑过使用 LINQ 吗?像这样(只是伪像,你必须在网上查找它,但只是为了有个想法)。

        XDocument xmlDoc = //load or parse with XDocument.Load(..) or XDocument.Parse(...)
        
        List<MyObject> myObj = (from myObject in xmlDoc.Descendants("myObjectTag")
                                select new MyObject
                                {
                                    Name = (string)myObject.Attribute("name"),
                                    ...
                                }
                                ).toList<MyObject>();
        

        瞧,这是我刚刚通过谷歌搜索找到的一篇博文:* click *

        【讨论】:

          猜你喜欢
          • 2012-03-04
          • 1970-01-01
          • 2015-07-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多