{"id":153,"date":"2025-11-24T14:49:49","date_gmt":"2025-11-24T06:49:49","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=153"},"modified":"2025-11-24T14:49:49","modified_gmt":"2025-11-24T06:49:49","slug":"c%e8%af%bb%e5%86%99xml%e6%96%87%e4%bb%b6%e5%ba%94%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2025\/11\/24\/c%e8%af%bb%e5%86%99xml%e6%96%87%e4%bb%b6%e5%ba%94%e7%94%a8\/","title":{"rendered":"C#\u8bfb\u5199xml\u6587\u4ef6\u5e94\u7528"},"content":{"rendered":"\n<p>\u4e0b\u9762\u7ed9\u4f60\u6574\u7406\u4e00\u4efd <strong>C# \u8bfb\u5199 XML \u6587\u4ef6\u5e94\u7528\u6559\u7a0b<\/strong>\uff0c\u6db5\u76d6\u57fa\u7840\u6982\u5ff5\u3001\u5e38\u7528\u7c7b\u3001\u8bfb\u53d6\u3001\u5199\u5165\u3001\u4fee\u6539\u53ca\u793a\u4f8b\uff0c\u9002\u5408\u5165\u95e8\u548c\u5b9e\u9645\u9879\u76ee\u4f7f\u7528\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udcd8 \u4e00\u3001C# \u64cd\u4f5c XML \u7684\u57fa\u7840<\/h1>\n\n\n\n<p>C# \u4e2d\u64cd\u4f5c XML \u6587\u4ef6\u5e38\u7528\u4e24\u79cd\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong><code>XmlDocument<\/code><\/strong>\uff08\u4f20\u7edf DOM \u65b9\u5f0f\uff09\n<ul class=\"wp-block-list\">\n<li>\u5c06\u6574\u4e2a XML \u52a0\u8f7d\u5230\u5185\u5b58\u4e2d<\/li>\n\n\n\n<li>\u652f\u6301\u8282\u70b9\u904d\u5386\u3001\u4fee\u6539\u3001\u63d2\u5165\u3001\u5220\u9664<\/li>\n\n\n\n<li>\u9002\u5408\u5c0f\u578b\u6216\u4e2d\u578b XML \u6587\u4ef6<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong><code>XDocument<\/code> \/ <code>LINQ to XML<\/code><\/strong>\uff08\u73b0\u4ee3\u65b9\u5f0f\uff09\n<ul class=\"wp-block-list\">\n<li>\u57fa\u4e8e LINQ \u67e5\u8be2\u548c\u64cd\u4f5c XML<\/li>\n\n\n\n<li>\u5199\u6cd5\u7b80\u6d01\uff0c\u53ef\u8bfb\u6027\u9ad8<\/li>\n\n\n\n<li>\u652f\u6301\u51fd\u6570\u5f0f\u64cd\u4f5c<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udcd8 \u4e8c\u3001XML \u6587\u4ef6\u793a\u4f8b<\/h1>\n\n\n\n<p>\u5047\u8bbe\u6709\u4e00\u4e2a\u6587\u4ef6 <code>books.xml<\/code>\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\n&amp;lt;library&gt;\n    &amp;lt;book id=&quot;b001&quot; category=&quot;Science&quot;&gt;\n        &amp;lt;title&gt;\u5b87\u5b99\u7b80\u53f2&amp;lt;\/title&gt;\n        &amp;lt;author&gt;Stephen Hawking&amp;lt;\/author&gt;\n        &amp;lt;price currency=&quot;USD&quot;&gt;68&amp;lt;\/price&gt;\n    &amp;lt;\/book&gt;\n    &amp;lt;book id=&quot;b002&quot; category=&quot;History&quot;&gt;\n        &amp;lt;title&gt;\u4eba\u7c7b\u7b80\u53f2&amp;lt;\/title&gt;\n        &amp;lt;author&gt;Yuval Noah Harari&amp;lt;\/author&gt;\n        &amp;lt;price currency=&quot;USD&quot;&gt;45&amp;lt;\/price&gt;\n    &amp;lt;\/book&gt;\n&amp;lt;\/library&gt;\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udcd8 \u4e09\u3001\u4f7f\u7528 <code>XmlDocument<\/code> \u8bfb\u53d6 XML<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nusing System;\nusing System.Xml;\n\nclass Program\n{\n    static void Main()\n    {\n        XmlDocument doc = new XmlDocument();\n        doc.Load(&quot;books.xml&quot;); \/\/ \u52a0\u8f7d XML \u6587\u4ef6\n\n        XmlNodeList books = doc.GetElementsByTagName(&quot;book&quot;);\n        foreach (XmlNode book in books)\n        {\n            string id = book.Attributes&#x5B;&quot;id&quot;].Value;\n            string category = book.Attributes&#x5B;&quot;category&quot;].Value;\n            string title = book&#x5B;&quot;title&quot;].InnerText;\n            string author = book&#x5B;&quot;author&quot;].InnerText;\n            string price = book&#x5B;&quot;price&quot;].InnerText;\n\n            Console.WriteLine($&quot;{id}, {category}, {title}, {author}, {price}&quot;);\n        }\n    }\n}\n\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><code>GetElementsByTagName<\/code>\uff1a\u83b7\u53d6\u6307\u5b9a\u6807\u7b7e\u7684\u8282\u70b9\u5217\u8868<\/li>\n\n\n\n<li><code>Attributes<\/code>\uff1a\u8bbf\u95ee\u5143\u7d20\u5c5e\u6027<\/li>\n\n\n\n<li><code>InnerText<\/code>\uff1a\u83b7\u53d6\u6587\u672c\u5185\u5bb9<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udcd8 \u56db\u3001\u4f7f\u7528 <code>XmlDocument<\/code> \u5199\u5165 XML<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nXmlDocument doc = new XmlDocument();\nXmlElement library = doc.CreateElement(&quot;library&quot;);\ndoc.AppendChild(library);\n\nXmlElement book = doc.CreateElement(&quot;book&quot;);\nbook.SetAttribute(&quot;id&quot;, &quot;b003&quot;);\nbook.SetAttribute(&quot;category&quot;, &quot;Technology&quot;);\n\nXmlElement title = doc.CreateElement(&quot;title&quot;);\ntitle.InnerText = &quot;C# \u7f16\u7a0b\u57fa\u7840&quot;;\nbook.AppendChild(title);\n\nXmlElement author = doc.CreateElement(&quot;author&quot;);\nauthor.InnerText = &quot;\u963f\u6770&quot;;\nbook.AppendChild(author);\n\nlibrary.AppendChild(book);\n\ndoc.Save(&quot;books_new.xml&quot;); \/\/ \u4fdd\u5b58 XML \u6587\u4ef6\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udcd8 \u4e94\u3001\u4f7f\u7528 <code>XDocument<\/code> \u8bfb\u53d6 XML\uff08LINQ \u98ce\u683c\uff09<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nusing System;\nusing System.Xml.Linq;\nusing System.Linq;\n\nclass Program\n{\n    static void Main()\n    {\n        XDocument doc = XDocument.Load(&quot;books.xml&quot;);\n        var books = doc.Descendants(&quot;book&quot;)\n                       .Select(b =&gt; new\n                       {\n                           Id = b.Attribute(&quot;id&quot;).Value,\n                           Category = b.Attribute(&quot;category&quot;).Value,\n                           Title = b.Element(&quot;title&quot;).Value,\n                           Author = b.Element(&quot;author&quot;).Value,\n                           Price = b.Element(&quot;price&quot;).Value\n                       });\n\n        foreach (var book in books)\n        {\n            Console.WriteLine($&quot;{book.Id}, {book.Category}, {book.Title}, {book.Author}, {book.Price}&quot;);\n        }\n    }\n}\n\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><code>Descendants<\/code> \u83b7\u53d6\u6307\u5b9a\u6807\u7b7e\u7684\u6240\u6709\u5b50\u5b59\u8282\u70b9<\/li>\n\n\n\n<li><code>Element<\/code> \u83b7\u53d6\u5b50\u5143\u7d20<\/li>\n\n\n\n<li><code>Attribute<\/code> \u83b7\u53d6\u5c5e\u6027<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udcd8 \u516d\u3001\u4f7f\u7528 <code>XDocument<\/code> \u5199\u5165 XML<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nXDocument doc = new XDocument(\n    new XElement(&quot;library&quot;,\n        new XElement(&quot;book&quot;,\n            new XAttribute(&quot;id&quot;, &quot;b004&quot;),\n            new XAttribute(&quot;category&quot;, &quot;Math&quot;),\n            new XElement(&quot;title&quot;, &quot;\u7ebf\u6027\u4ee3\u6570&quot;),\n            new XElement(&quot;author&quot;, &quot;\u5f20\u4e09&quot;),\n            new XElement(&quot;price&quot;, new XAttribute(&quot;currency&quot;,&quot;CNY&quot;), &quot;88&quot;)\n        )\n    )\n);\n\ndoc.Save(&quot;books_linq.xml&quot;);\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udcd8 \u4e03\u3001XML \u6587\u4ef6\u4fee\u6539\u793a\u4f8b<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/\/ \u4fee\u6539 book b001 \u7684\u4ef7\u683c\nXDocument doc = XDocument.Load(&quot;books.xml&quot;);\nvar book = doc.Descendants(&quot;book&quot;)\n              .FirstOrDefault(b =&gt; b.Attribute(&quot;id&quot;).Value == &quot;b001&quot;);\nif(book != null)\n{\n    book.Element(&quot;price&quot;).Value = &quot;70&quot;; \/\/ \u4fee\u6539\u4ef7\u683c\n}\ndoc.Save(&quot;books_modified.xml&quot;);\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udcd8 \u516b\u3001\u603b\u7ed3<\/h1>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u9009\u62e9\u65b9\u5f0f<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u5c0f\u6587\u4ef6\u3001\u9700\u8981\u9891\u7e41\u904d\u5386\u3001\u4fee\u6539\uff1a<code>XmlDocument<\/code><\/li>\n\n\n\n<li>\u5bf9\u8c61\u5316\u3001\u53ef\u8bfb\u6027\u9ad8\u3001LINQ \u67e5\u8be2\u65b9\u4fbf\uff1a<code>XDocument<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u64cd\u4f5c\u6b65\u9aa4<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u52a0\u8f7d XML \u6587\u4ef6<\/li>\n\n\n\n<li>\u83b7\u53d6\u8282\u70b9\u6216\u5c5e\u6027<\/li>\n\n\n\n<li>\u8bfb\u53d6\u3001\u4fee\u6539\u3001\u6dfb\u52a0\u6216\u5220\u9664\u5143\u7d20<\/li>\n\n\n\n<li>\u4fdd\u5b58\u6587\u4ef6<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u5b9e\u8df5\u5efa\u8bae<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u8bfb\u5199\u5927\u578b XML \u6587\u4ef6\u53ef\u8003\u8651 <code>XmlReader<\/code>\/<code>XmlWriter<\/code> \u63d0\u9ad8\u6027\u80fd<\/li>\n\n\n\n<li>\u4f7f\u7528 LINQ \u53ef\u7b80\u5316\u6570\u636e\u67e5\u8be2\u548c\u7b5b\u9009<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u7ed9\u4f60\u6574\u7406\u4e00\u4efd C# \u8bfb\u5199 XML \u6587\u4ef6\u5e94\u7528\u6559\u7a0b\uff0c\u6db5\u76d6\u57fa\u7840\u6982\u5ff5\u3001\u5e38\u7528\u7c7b\u3001\u8bfb\u53d6\u3001&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2025\/11\/24\/c%e8%af%bb%e5%86%99xml%e6%96%87%e4%bb%b6%e5%ba%94%e7%94%a8\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-153","post","type-post","status-publish","format-standard","hentry","category-xml-xslt"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/153","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=153"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/153\/revisions"}],"predecessor-version":[{"id":154,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/153\/revisions\/154"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}