{"id":579,"date":"2009-08-16T22:16:00","date_gmt":"2009-08-16T02:16:00","guid":{"rendered":""},"modified":"2013-11-17T12:51:30","modified_gmt":"2013-11-17T04:51:30","slug":"asp-net%e6%93%8d%e4%bd%9cxml%e6%96%87%e4%bb%b6%e7%9a%84%e4%bb%a3%e7%a0%81%e5%ae%9e%e4%be%8b","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/579.html","title":{"rendered":"ASP.NET\u64cd\u4f5cxml\u6587\u4ef6\u7684\u4ee3\u7801\u5b9e\u4f8b"},"content":{"rendered":"<p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u5148\u5efa\u7acb\u4e00\u4e2asmallfools.xml\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\r\n&lt;smallfoolsRoot&gt;\r\n   &lt;poems&gt;\r\n     &lt;author&gt;\u738b\u7ef4&lt;\/author&gt;\r\n     &lt;tITle&gt;\u7af9\u91cc\u9986&lt;\/title&gt;\r\n     &lt;content&gt;\u72ec\u5750\u5e7d\u7bc1\u91cc\uff0c\u5f39\u7434\u590d\u957f\u5578\u3002\u6df1\u6797\u4eba\u4e0d\u77e5\uff0c\u660e\u6708\u6765\u76f8\u7167\u3002&lt;\/content&gt;\r\n   &lt;\/poems&gt;\r\n   &lt;poems&gt;\r\n     &lt;author&gt;\u5b5f\u6d69\u7136&lt;\/author&gt;\r\n     &lt;title&gt;\u5bbf\u5efa\u5fb7\u6c5f&lt;\/title&gt;\r\n     &lt;content&gt;\u79fb\u821f\u6cca\u70df\u6e1a\uff0c\u65e5\u66ae\u5ba2\u6101\u65b0\u3002\u91ce\u65f7\u5929\u4f4e\u6811\uff0c\u6c5f\u6e05\u6708\u8fd1\u4eba&lt;\/content&gt;\r\n   &lt;\/poems&gt;\r\n   &lt;poems&gt;\r\n     &lt;author&gt;\u674e\u767d&lt;\/author&gt;\r\n     &lt;title&gt;\u675c\u9675\u7edd\u53e5&lt;\/title&gt;\r\n     &lt;content&gt;\u5357\u767b\u675c\u9675\u4e0a\uff0c\u5317\u671b\u4e94\u9675\u95f4\u3002\u79cb\u6c34\u660e\u843d\u65e5\uff0c\u6d41\u5149\u706d\u8fdc\u5c71&lt;\/content&gt;\r\n   &lt;\/poems&gt;\r\n   &lt;poems&gt;\r\n     &lt;author&gt;\u674e\u767d&lt;\/author&gt;\r\n     &lt;title&gt;\u671b\u5e90\u5c71\u7011\u5e03&lt;\/title&gt;\r\n     &lt;content&gt;\u65e5\u7167\u9999\u7089\u751f\u7d2b\u70df\uff0c\u9065\u770b\u7011\u5e03\u6302\u524d\u5ddd\u3002\u98de\u6d41\u76f4\u4e0b\u4e09\u5343\u5c3a\uff0c\u7591\u662f\u94f6\u6cb3\u843d\u4e5d\u5929\u3002&lt;\/content&gt;\r\n   &lt;\/poems&gt;\r\n   &lt;poems&gt;\r\n     &lt;author&gt;\u674e\u5546\u9690&lt;\/author&gt;\r\n     &lt;title&gt;\u9526\u745f&lt;\/title&gt;\r\n     &lt;content&gt;\u9526\u745f\u65e0\u7aef\u4e94\u5341\u5f26\uff0c\u4e00\u5f26\u4e00\u67f1\u601d\u534e\u5e74\u3002\u5e84\u751f\u6653\u68a6\u8ff7\u8774\u8776\uff0c\u671b\u5e1d\u6625\u5fc3\u6258\u675c\u9e43\u3002\u6ca7\u6d77\u6708\u660e\u73e0\u6709\u6cea\uff0c\u84dd\u7530\u65e5\u6696\u7389\u751f\u70df\u3002\u6b64\u60c5\u53ef\u5f85\u6210\u8ffd\u5fc6\uff0c\u53ea\u662f\u5f53\u65f6\u5df2\u60d8\u7136\u3002&lt;\/content&gt;\r\n   &lt;\/poems&gt;\r\n&lt;\/smallfoolsRoot&gt; \r\n\r\n<\/pre>\n<p>\u4e0b\u9762\u7684\u64cd\u4f5c\u90fd\u5728\u8fd9\u4e2axml\u6587\u4ef6\u91cc\u8fdb\u884c\u3002<\/p>\n<p>\u64cd\u4f5c\u4e00\uff1a\u8bfb\u53d6\u6574\u4e2aXML\u6587\u4ef6\uff0c\u5e76\u5728DataGrid\u91cc\u663e\u793a\u51fa\u6765\uff1a<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nDataSet ds = new DataSet();\r\nds.ReadXml(Server.MapPath(&quot;smallfools.xml&quot;));\r\nif (ds.Tables.Count&gt;0)\r\n{\r\nthis.DataGrid1.DataSource = ds.Tables&#x5B;0].DefaultView;\r\nthis.DataGrid1.DataBind();\r\n}\r\n<\/pre>\n<p>\u64cd\u4f5c\u4e8c\uff1a\u83b7\u5f97\u7b2c\u4e00\u4e2a\u8282\u70b9\u7684\u503c<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nXmlDocument xmlDoc = new XmlDocument();\r\nxmlDoc.Load(Server.MapPath(&quot;smallfools.xml&quot;));\r\nXmlNode xmlNode = xmlDoc.DocumentElement.FirstChild;\r\nif (xmlNode!=null)\r\n{\r\nthis.tbauthor.Text = xmlNode&#x5B;&quot;author&quot;].InnerText;\r\nthis.tbtitle.Text = xmlNode&#x5B;&quot;title&quot;].InnerText;\r\nthis.tbcontent.Text = xmlNode&#x5B;&quot;content&quot;].InnerText;\r\nViewState&#x5B;&quot;Count&quot;] = 0;\r\n}\r\n<\/pre>\n<p>\u64cd\u4f5c\u4e09\uff1a\u67e5\u770b\u67d0\u4e00\u4e2a\u8282\u70b9\u7684\u5185\u5bb9<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nXmlDocument xmlDoc = new XmlDocument();\r\nxmlDoc.Load(Server.MapPath(&quot;smallfools.xml&quot;));\r\nXmlNodeList xmlNodeList = xmlDoc.DocumentElement.ChildNodes;\r\nXmlNode xmlNode = xmlNodeList.Item(0);\r\nthis.tbauthor.Text = xmlNode&#x5B;&quot;author&quot;].InnerText;\r\nthis.tbtitle.Text = xmlNode&#x5B;&quot;title&quot;].InnerText;\r\nthis.tbcontent.Text = xmlNode&#x5B;&quot;content&quot;].InnerText;\r\n<\/pre>\n<p>\u64cd\u4f5c\u56db\uff1a\u6dfb\u52a0\u4e00\u4e2a\u8282\u70b9<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nXmlDocument xmlDoc = new XmlDocument();\r\nxmlDoc.Load(Server.MapPath(&quot;smallfools.xml&quot;));\r\n\/\/\u521b\u5efa\u4e00\u4e2a\u65b0\u8282\u70b9\r\nXmlElement newElement = xmlDoc.CreateElement(&quot;poems&quot;);\r\n\/\/\u521b\u5efanewElement\u4e0b\u7684\u8282\u70b9\r\nXmlElement elauthor = xmlDoc.CreateElement(&quot;author&quot;);\r\nXmlElement eltitle = xmlDoc.CreateElement(&quot;title&quot;);\r\nXmlElement elcontent = xmlDoc.CreateElement(&quot;content&quot;);\r\nelauthor.InnerText = this.tbaddauthor.Text.Trim();\r\neltitle.InnerText = this.tbaddtitle.Text.Trim();\r\nelcontent.InnerText = this.tbaddcontent.Text.Trim();\r\n\/\/\u5c06newElement\u4e0b\u7684\u8282\u70b9\u52a0\u5230newElement\u4e0a\r\nnewElement.AppendChild(elauthor);\r\nnewElement.AppendChild(eltitle);\r\nnewElement.AppendChild(elcontent);\r\n\/\/\u5c06newElement\u52a0\u5165\u5230xml\u6587\u4ef6\u4e2d\uff08\u52a0\u5728\u6700\u540e\u4e00\u6761\u8bb0\u5f55\u4e0a\uff09\r\nxmlDoc.DocumentElement.AppendChild(newElement);\r\n\/\/\u5982\u679c\u8981\u63d2\u5230\u67d0\u6761\u8bb0\u5f55\u4e4b\u540e\u4e5f\u53ef\u4ee5\u7528\uff08\u52a0\u5728\u7b2c\u4e00\u6761\u8bb0\u5f55\u4e4b\u540e\uff09\r\n\/\/xmlDoc.DocumentElement.InsertAfter(newElement,xmlDoc.DocumentElement.ChildNodes.Item(0));\r\n\/\/\u5982\u679c\u8981\u63d2\u5230\u67d0\u6761\u8bb0\u5f55\u4e4b\u524d\u4e5f\u53ef\u4ee5\u7528\uff08\u52a0\u5728\u7b2c\u4e00\u6761\u8bb0\u5f55\u4e4b\u524d\uff09\r\n\/\/xmlDoc.DocumentElement.InsertBefore(newElement,xmlDoc.DocumentElement.ChildNodes.Item(0));\r\n\/\/\u5b58\u76d8\r\nxmlDoc.Save(Server.MapPath(&quot;smallfools.xml&quot;));\r\n<\/pre>\n<p>\u64cd\u4f5c\u4e94\uff1a\u5220\u9664\u67d0\u4e2a\u8282\u70b9<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nXmlDocument xmlDoc = new XmlDocument();\r\nxmlDoc.Load(Server.MapPath(&quot;smallfools.xml&quot;));\r\nXmlNode xmlNode = xmlDoc.DocumentElement.ChildNodes.Item(0);\r\nxmlNode.ParentNode.RemoveChild(xmlNode);\r\nxmlDoc.Save(Server.MapPath(&quot;smallfools.xml&quot;));\r\n<\/pre>\n<p>\u64cd\u4f5c\u516d\uff1a\u7f16\u8f91\u67d0\u4e2a\u8282\u70b9<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nXmlDocument xmlDoc = new XmlDocument();\r\nxmlDoc.Load(Server.MapPath(&quot;smallfools.xml&quot;));\r\n\/\/\u83b7\u5f97\u8282\u70b9\u5217\u8868\r\nXmlNode xmlNode = xmlDoc.DocumentElement.ChildNodes.Item(1);\r\nxmlNode&#x5B;&quot;author&quot;].InnerText = this.tbauthor.Text;\r\nxmlNode&#x5B;&quot;title&quot;].InnerText = this.tbtitle.Text;\r\nxmlNode&#x5B;&quot;content&quot;].InnerText = this.tbcontent.Text;\r\nxmlDoc.Save(Server.MapPath(&quot;smallfools.xml&quot;));\r\n<\/pre>\n<p>\u64cd\u4f5c\u4e03\uff1a\u67e5\u627e\u8bb0\u5f55<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nXmlDocument xmlDoc = new XmlDocument();\r\nxmlDoc.Load(Server.MapPath(&quot;smallfools.xml&quot;));\r\nXmlNodeList nodelist = xmlDoc.SelectNodes(&quot;smallfoolsRoot\/poems&#x5B;author='&quot;+this.tbsearch.Text.Trim()+&quot;']&quot;);\r\n<\/pre>\n<p>\u64cd\u4f5c\u516b\uff1a\u7cca\u6a21\u67e5\u627e\u8bb0\u5f55<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nXmlDocument xmlDoc = new XmlDocument();\r\nxmlDoc.Load(Server.MapPath(&quot;smallfools.xml&quot;));\r\nXmlNodeList nodelist = xmlDoc.SelectNodes(&quot;smallfoolsRoot\/poems&#x5B;contains(author,'&quot;+this.tbsearch.Text.Trim()+&quot;')]&quot;);\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u5148\u5efa\u7acb\u4e00\u4e2asmallfools.xml\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a &lt;?xml version=&#038;quo [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-579","post","type-post","status-publish","format-standard","hentry","category-code_related"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/579","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=579"}],"version-history":[{"count":3,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/579\/revisions"}],"predecessor-version":[{"id":4604,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/579\/revisions\/4604"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=579"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=579"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=579"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}