<small id='TmIvd'></small><noframes id='TmIvd'>

<i id='TmIvd'><tr id='TmIvd'><dt id='TmIvd'><q id='TmIvd'><span id='TmIvd'><b id='TmIvd'><form id='TmIvd'><ins id='TmIvd'></ins><ul id='TmIvd'></ul><sub id='TmIvd'></sub></form><legend id='TmIvd'></legend><bdo id='TmIvd'><pre id='TmIvd'><center id='TmIvd'></center></pre></bdo></b><th id='TmIvd'></th></span></q></dt></tr></i><div id='TmIvd'><tfoot id='TmIvd'></tfoot><dl id='TmIvd'><fieldset id='TmIvd'></fieldset></dl></div>

      <bdo id='TmIvd'></bdo><ul id='TmIvd'></ul>

    1. <tfoot id='TmIvd'></tfoot>

      <legend id='TmIvd'><style id='TmIvd'><dir id='TmIvd'><q id='TmIvd'></q></dir></style></legend>
    2. 编写xml并读回c#

      时间:2023-08-26
        <tbody id='r0E45'></tbody>

      • <tfoot id='r0E45'></tfoot>

        <small id='r0E45'></small><noframes id='r0E45'>

        1. <i id='r0E45'><tr id='r0E45'><dt id='r0E45'><q id='r0E45'><span id='r0E45'><b id='r0E45'><form id='r0E45'><ins id='r0E45'></ins><ul id='r0E45'></ul><sub id='r0E45'></sub></form><legend id='r0E45'></legend><bdo id='r0E45'><pre id='r0E45'><center id='r0E45'></center></pre></bdo></b><th id='r0E45'></th></span></q></dt></tr></i><div id='r0E45'><tfoot id='r0E45'></tfoot><dl id='r0E45'><fieldset id='r0E45'></fieldset></dl></div>

                <bdo id='r0E45'></bdo><ul id='r0E45'></ul>
              • <legend id='r0E45'><style id='r0E45'><dir id='r0E45'><q id='r0E45'></q></dir></style></legend>
              • 本文介绍了编写xml并读回c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                好的,我现在使用文档方法而不是 XmlWriter 来编写我的 XML.我已经编写了我的 XML 文件.

                ok, I am now using the document method for writing my XML instead of the XmlWriter. I have written my XML file with.

                userNode = xmlDoc.CreateElement("user");
                attribute = xmlDoc.CreateAttribute("age");
                attribute.Value = "39";
                userNode.Attributes.Append(attribute);
                userNode.InnerText = "Jane Doe";
                rootNode.AppendChild(userNode);
                

                但问题又是如何读取这些设置.

                But the question is again how to read these settings back.

                <users>
                  <user name="John Doe" age="42" />
                  <user name="Jane Doe" age="39" />
                </users>
                

                文件的格式我可以弄清楚如何读取 age 变量,但无法掌握 name 属性.我的 XML 文件与上面略有不同,但差别不大

                The format of the file I can figure out how to read the age variable but can't get my hands on the name property. my XML file is slightly different to above but not by much

                推荐答案

                逐个元素地编写 XML 文件可能非常耗时 - 并且容易出错.

                Writing XML files element by element can be quite time consuming - and susceptible to errors.

                我建议对这类工作使用 XML 序列化器.

                I would suggest using an XML Serializer for this type of job.

                如果您不关心格式 - 并且要求只是能够序列化为 XML 并在以后反序列化,则代码可以简单如下:

                If you are not concerned with the format - and the requirement is just to be able to serialize to XML and deserialize at a later time the code can be as simple as follows:

                public class User
                {
                    public string Name { get; set; }
                    public int Age { get; set; }
                }
                
                string filepath = @"c:	empusers.xml";
                
                var usersToStore = new List<User>
                {
                     new User { Name = "John Doe", Age = 42 },
                     new User { Name = "Jane Doe", Age = 29 }
                };
                
                using (FileStream fs = new FileStream(filepath, FileMode.OpenOrCreate))
                {
                    XmlSerializer serializer = new XmlSerializer(usersToStore.GetType());
                    serializer.Serialize(fs, usersToStore);
                }
                
                 var retrievedUsers = new List<User>();
                 using (FileStream fs2 = new FileStream(filepath, FileMode.Open))
                 {
                     XmlSerializer serializer = new XmlSerializer(usersToStore.GetType());
                     retrievedUsers = serializer.Deserialize(fs2) as List<User>;
                 }
                

                Microsoft 在 .Net 文档中提供了一些很好的示例- 介绍 XML 序列化

                Microsoft provides some good examples in the .Net documentation - Introducing XML Serialization

                这篇关于编写xml并读回c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:使用 C#、LINQ 从 xml 字符串中读取子节点 下一篇:C# 将根添加到 XDocument

                相关文章

                最新文章

              • <small id='gIzGt'></small><noframes id='gIzGt'>

                      <bdo id='gIzGt'></bdo><ul id='gIzGt'></ul>

                  1. <tfoot id='gIzGt'></tfoot>
                  2. <legend id='gIzGt'><style id='gIzGt'><dir id='gIzGt'><q id='gIzGt'></q></dir></style></legend>
                    <i id='gIzGt'><tr id='gIzGt'><dt id='gIzGt'><q id='gIzGt'><span id='gIzGt'><b id='gIzGt'><form id='gIzGt'><ins id='gIzGt'></ins><ul id='gIzGt'></ul><sub id='gIzGt'></sub></form><legend id='gIzGt'></legend><bdo id='gIzGt'><pre id='gIzGt'><center id='gIzGt'></center></pre></bdo></b><th id='gIzGt'></th></span></q></dt></tr></i><div id='gIzGt'><tfoot id='gIzGt'></tfoot><dl id='gIzGt'><fieldset id='gIzGt'></fieldset></dl></div>