{"id":134,"date":"2008-12-16T10:57:55","date_gmt":"2008-12-16T10:57:55","guid":{"rendered":""},"modified":"2008-12-16T10:57:55","modified_gmt":"2008-12-16T10:57:55","slug":"134","status":"publish","type":"post","link":"https:\/\/www.poloo.org\/?p=134","title":{"rendered":"C#\u8bfb\u5199xml\u6587\u4ef6"},"content":{"rendered":"<p>\u5df2\u77e5\u6709\u4e00\u4e2aXML\u6587\u4ef6\uff08bookstore.xml\uff09\u5982\u4e0b\uff1a<br \/><code><br \/>&lt;?xml version=\"1.0\" encoding=\"gb2312\"?&gt;<br \/>&lt;bookstore&gt;<br \/>&nbsp;&nbsp;&lt;book genre=\"fantasy\" ISBN=\"2-3631-4\"&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;title&gt;Oberon's Legacy&lt;\/title&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;author&gt;Corets, Eva&lt;\/author&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;price&gt;5.95&lt;\/price&gt;<br \/>&nbsp;&nbsp;&lt;\/book&gt;<br \/>&lt;\/bookstore&gt;<br \/> <\/code><\/p>\n<p>1\u3001\u5f80&lt;bookstore&gt;\u8282\u70b9\u4e2d\u63d2\u5165\u4e00\u4e2a&lt;book&gt;\u8282\u70b9\uff1a<br \/>[\/code]<br \/>&nbsp;&nbsp; XmlDocument xmlDoc=new XmlDocument();<br \/>&nbsp;&nbsp; xmlDoc.Load(&#8220;bookstore.xml&#8221;);<br \/>&nbsp;&nbsp; XmlNode root=xmlDoc.SelectSingleNode(&#8220;bookstore&#8221;);\/\/\u67e5\u627e&lt;bookstore&gt;<br \/>&nbsp;&nbsp; XmlElement xe1=xmlDoc.CreateElement(&#8220;book&#8221;);\/\/\u521b\u5efa\u4e00\u4e2a&lt;book&gt;\u8282\u70b9<br \/>&nbsp;&nbsp; xe1.SetAttribute(&#8220;genre&#8221;,&#8221;\u674e\u8d5e\u7ea2&#8221;);\/\/\u8bbe\u7f6e\u8be5\u8282\u70b9genre\u5c5e\u6027<br \/>&nbsp;&nbsp; xe1.SetAttribute(&#8220;ISBN&#8221;,&#8221;2-3631-4&#8243;);\/\/\u8bbe\u7f6e\u8be5\u8282\u70b9ISBN\u5c5e\u6027<\/p>\n<p>&nbsp;&nbsp; XmlElement xesub1=xmlDoc.CreateElement(&#8220;title&#8221;);<br \/>&nbsp;&nbsp; xesub1.InnerText=&#8221;CS\u4ece\u5165\u95e8\u5230\u7cbe\u901a&#8221;;\/\/\u8bbe\u7f6e\u6587\u672c\u8282\u70b9<br \/>&nbsp;&nbsp; xe1.AppendChild(xesub1);\/\/\u6dfb\u52a0\u5230&lt;book&gt;\u8282\u70b9\u4e2d<br \/>&nbsp;&nbsp; XmlElement xesub2=xmlDoc.CreateElement(&#8220;author&#8221;);<br \/>&nbsp;&nbsp; xesub2.InnerText=&#8221;\u5019\u6377&#8221;;<br \/>&nbsp;&nbsp; xe1.AppendChild(xesub2);<br \/>&nbsp;&nbsp; XmlElement xesub3=xmlDoc.CreateElement(&#8220;price&#8221;);<br \/>&nbsp;&nbsp; xesub3.InnerText=&#8221;58.3&#8243;;<br \/>&nbsp;&nbsp; xe1.AppendChild(xesub3);<\/p>\n<p>&nbsp;&nbsp; root.AppendChild(xe1);\/\/\u6dfb\u52a0\u5230&lt;bookstore&gt;\u8282\u70b9\u4e2d<br \/>&nbsp;&nbsp; xmlDoc.Save(&#8220;bookstore.xml&#8221;);<br \/>[\/code]<\/p>\n<p>\/\/===============================================<br \/>\u7ed3\u679c\u4e3a\uff1a<br \/><code><br \/>&lt;?xml version=\"1.0\" encoding=\"gb2312\"?&gt;<br \/>&lt;bookstore&gt;<br \/>&nbsp;&nbsp;&lt;book genre=\"fantasy\" ISBN=\"2-3631-4\"&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;title&gt;Oberon's Legacy&lt;\/title&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;author&gt;Corets, Eva&lt;\/author&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;price&gt;5.95&lt;\/price&gt;<br \/>&nbsp;&nbsp;&lt;\/book&gt;<br \/>&nbsp;&nbsp;&lt;book genre=\"\u674e\u8d5e\u7ea2\" ISBN=\"2-3631-4\"&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;title&gt;CS\u4ece\u5165\u95e8\u5230\u7cbe\u901a&lt;\/title&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;author&gt;\u5019\u6377&lt;\/author&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;price&gt;58.3&lt;\/price&gt;<br \/>&nbsp;&nbsp;&lt;\/book&gt;<br \/>&lt;\/bookstore&gt;<br \/><\/code> <\/p>\n<p>2\u3001\u4fee\u6539\u8282\u70b9\uff1a\u5c06genre\u5c5e\u6027\u503c\u4e3a\u201c\u674e\u8d5e\u7ea2\u201c\u7684\u8282\u70b9\u7684genre\u503c\u6539\u4e3a\u201cupdate\u674e\u8d5e\u7ea2\u201d\uff0c\u5c06\u8be5\u8282\u70b9\u7684\u5b50\u8282\u70b9&lt;author&gt;\u7684\u6587\u672c\u4fee\u6539\u4e3a\u201c\u4e9a\u80dc\u201d\u3002<br \/><code><br \/>&nbsp;&nbsp;&nbsp;&nbsp;XmlNodeList nodeList=xmlDoc.SelectSingleNode(\"bookstore\").ChildNodes;\/\/\u83b7\u53d6bookstore\u8282\u70b9\u7684\u6240\u6709\u5b50\u8282\u70b9<br \/>&nbsp;&nbsp; foreach(XmlNode xn in nodeList)\/\/\u904d\u5386\u6240\u6709\u5b50\u8282\u70b9<br \/>&nbsp;&nbsp; {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;XmlElement xe=(XmlElement)xn;\/\/\u5c06\u5b50\u8282\u70b9\u7c7b\u578b\u8f6c\u6362\u4e3aXmlElement\u7c7b\u578b<br \/>&nbsp;&nbsp;&nbsp;&nbsp;if(xe.GetAttribute(\"genre\")==\"\u674e\u8d5e\u7ea2\")\/\/\u5982\u679cgenre\u5c5e\u6027\u503c\u4e3a\u201c\u674e\u8d5e\u7ea2\u201d<br \/>&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp; xe.SetAttribute(\"genre\",\"update\u674e\u8d5e\u7ea2\");\/\/\u5219\u4fee\u6539\u8be5\u5c5e\u6027\u4e3a\u201cupdate\u674e\u8d5e\u7ea2\u201d<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp; XmlNodeList nls=xe.ChildNodes;\/\/\u7ee7\u7eed\u83b7\u53d6xe\u5b50\u8282\u70b9\u7684\u6240\u6709\u5b50\u8282\u70b9<br \/>&nbsp;&nbsp;&nbsp;&nbsp; foreach(XmlNode xn1 in nls)\/\/\u904d\u5386<br \/>&nbsp;&nbsp;&nbsp;&nbsp; {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;XmlElement xe2=(XmlElement)xn1;\/\/\u8f6c\u6362\u7c7b\u578b<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(xe2.Name==\"author\")\/\/\u5982\u679c\u627e\u5230<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xe2.InnerText=\"\u4e9a\u80dc\";\/\/\u5219\u4fee\u6539<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;\/\/\u627e\u5230\u9000\u51fa\u6765\u5c31\u53ef\u4ee5\u4e86<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp; }<br \/>&nbsp;&nbsp;&nbsp;&nbsp; break;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp; }<\/p>\n<p>&nbsp;&nbsp; xmlDoc.Save(\"bookstore.xml\");\/\/\u4fdd\u5b58\u3002<br \/><\/code><\/p>\n<p>\/\/==================================================<br \/>\u6700\u540e\u7ed3\u679c\u4e3a\uff1a<br \/><code><br \/>&lt;?xml version=\"1.0\" encoding=\"gb2312\"?&gt;<br \/>&lt;bookstore&gt;<br \/>&nbsp;&nbsp;&lt;book genre=\"fantasy\" ISBN=\"2-3631-4\"&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;title&gt;Oberon's Legacy&lt;\/title&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;author&gt;Corets, Eva&lt;\/author&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;price&gt;5.95&lt;\/price&gt;<br \/>&nbsp;&nbsp;&lt;\/book&gt;<br \/>&nbsp;&nbsp;&lt;book genre=\"update\u674e\u8d5e\u7ea2\" ISBN=\"2-3631-4\"&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;title&gt;CS\u4ece\u5165\u95e8\u5230\u7cbe\u901a&lt;\/title&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;author&gt;\u4e9a\u80dc&lt;\/author&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;price&gt;58.3&lt;\/price&gt;<br \/>&nbsp;&nbsp;&lt;\/book&gt;<br \/>&lt;\/bookstore&gt;<br \/><\/code> <\/p>\n<p>3\u3001\u5220\u9664 &lt;book genre=&#8221;fantasy&#8221; ISBN=&#8221;2-3631-4&#8243;&gt;\u8282\u70b9\u7684genre\u5c5e\u6027\uff0c\u5220\u9664 &lt;book genre=&#8221;update\u674e\u8d5e\u7ea2&#8221; ISBN=&#8221;2-3631-4&#8243;&gt;\u8282\u70b9\u3002<br \/><code><br \/>XmlNodeList xnl=xmlDoc.SelectSingleNode(\"bookstore\").ChildNodes;<\/p>\n<p>&nbsp;&nbsp; foreach(XmlNode xn in xnl)<br \/>&nbsp;&nbsp; {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;XmlElement xe=(XmlElement)xn;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;if(xe.GetAttribute(\"genre\")==\"fantasy\")<br \/>&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp; xe.RemoveAttribute(\"genre\");\/\/\u5220\u9664genre\u5c5e\u6027<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;else if(xe.GetAttribute(\"genre\")==\"update\u674e\u8d5e\u7ea2\")<br \/>&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp; xe.RemoveAll();\/\/\u5220\u9664\u8be5\u8282\u70b9\u7684\u5168\u90e8\u5185\u5bb9<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp; }<br \/>&nbsp;&nbsp; xmlDoc.Save(\"bookstore.xml\");<br \/><\/code><\/p>\n<p>\/\/===========================================<br \/>\u6700\u540e\u7ed3\u679c\u4e3a\uff1a<br \/><code><br \/>&lt;?xml version=\"1.0\" encoding=\"gb2312\"?&gt;<br \/>&lt;bookstore&gt;<br \/>&nbsp;&nbsp;&lt;book ISBN=\"2-3631-4\"&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;title&gt;Oberon's Legacy&lt;\/title&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;author&gt;Corets, Eva&lt;\/author&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;price&gt;5.95&lt;\/price&gt;<br \/>&nbsp;&nbsp;&lt;\/book&gt;<br \/>&nbsp;&nbsp;&lt;book&gt;<br \/>&nbsp;&nbsp;&lt;\/book&gt;<br \/>&lt;\/bookstore&gt;<br \/><\/code> <\/p>\n<p>4\u3001\u663e\u793a\u6240\u6709\u6570\u636e\u3002<br \/><code><br \/>&nbsp;&nbsp; XmlNode xn=xmlDoc.SelectSingleNode(\"bookstore\");<\/p>\n<p>&nbsp;&nbsp; XmlNodeList xnl=xn.ChildNodes;<br \/>&nbsp;&nbsp; <br \/>&nbsp;&nbsp; foreach(XmlNode xnf in xnl)<br \/>&nbsp;&nbsp; {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;XmlElement xe=(XmlElement)xnf;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(xe.GetAttribute(\"genre\"));\/\/\u663e\u793a\u5c5e\u6027\u503c<br \/>&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(xe.GetAttribute(\"ISBN\"));<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;XmlNodeList xnf1=xe.ChildNodes;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;foreach(XmlNode xn2 in xnf1)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine(xn2.InnerText);\/\/\u663e\u793a\u5b50\u8282\u70b9\u70b9\u6587\u672c<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp; } <br \/><\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5df2\u77e5\u6709\u4e00\u4e2aXML\u6587\u4ef6\uff08bookstore.xml\uff09\u5982\u4e0b\uff1a&lt;?x&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-134","post","type-post","status-publish","format-standard","hentry","category-NET"],"_links":{"self":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/134","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=134"}],"version-history":[{"count":0,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/134\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}