ホームページ  >  記事  >  バックエンド開発  >  XmlDocument オブジェクト操作の詳細な手順

XmlDocument オブジェクト操作の詳細な手順

PHPz
PHPzオリジナル
2017-03-12 15:55:462180ブラウズ

XmlReader を使用してドキュメントを走査するのは非常に便利です。また、XmlWriter を使用して新しい XML ドキュメントを生成することも簡単ですが、要素の追加やファイルの変更など、既存の XML を変更するのはさらに面倒です。このとき、XmlDocumentオブジェクトを使用してDOMメソッドを呼び出してドキュメントを変更し、DOMが標準化されているため、多くの複数の言語が使用できます。 JS など、これをサポートしているため、GetElementByID()、GetElementByTagName()、AppendChild()、InsertAfter() など、ここにある多くのメソッドは JS と一致しています。 1 XML ファイルを作成します。

XmlDocument doc = new XmlDocument();
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(dec);
 XmlElement root = doc.CreateElement("bookstore");//创建根节点
doc.AppendChild(root);
 XmlElement newBook = _doc.CreateElement("book");//create a new 'book' element
//set some attributes
newBook.SetAttribute("genre", "Mystery");      newBook.SetAttribute("publicationdate", "2001");      newBook.SetAttribute("ISBN", "123456789");
//create a new 'title' element
XmlElement newTitle = _doc.CreateElement("title");      newTitle.InnerText = "Case of the Missing Cookie";      newBook.AppendChild(newTitle);
 //create new author element
XmlElement newAuthor = _doc.CreateElement("author");      newBook.AppendChild(newAuthor);
//create new name element      XmlElement newName = _doc.CreateElement("name");      newName.InnerText = "Cookie Monster";      newAuthor.AppendChild(newName);
     //create new price element      XmlElement newPrice = _doc.CreateElement("price");      newPrice.InnerText = "9.95";      newBook.AppendChild(newPrice);      //add to the current documentdoc.DocumentElement.AppendChild(newBook);//_doc.DocumentElement为获取xml的根
doc.Save("bb.xml");将 XML 文档保存到指定的位置

2. ノードの挿入はxmlファイルの作成と同様です

_doc.Load("books.xml");      XmlElement newBook = _doc.CreateElement("book");      newBook.SetAttribute("genre", "Mystery");      newBook.SetAttribute("publicationdate", "2001");      newBook.SetAttribute("ISBN", "123456789");      XmlElement newTitle = _doc.CreateElement("title");      newTitle.InnerText = "Case of the Missing Cookie";      newBook.AppendChild(newTitle);      XmlElement newAuthor = _doc.CreateElement("author");      newBook.AppendChild(newAuthor);      XmlElement newName = _doc.CreateElement("name");      newName.InnerText = "Cookie Monster";      newAuthor.AppendChild(newName);      XmlElement newPrice = _doc.CreateElement("price");      newPrice.InnerText = "9.95";      newBook.AppendChild(newPrice);      _doc.DocumentElement.AppendChild(newBook);
_doc.Save("booksEdit.xml");
或者下面这样保存      XmlTextWriter tr = new XmlTextWriter("booksEdit.xml", null);//将xml文档保存,如果存在此文件,则覆盖      tr.Formatting = Formatting.Indented;      _doc.WriteContentTo(tr);
tr.Close();

3. XMLノードを変更します

ジャンル属性値が「novel」のノードのジャンル値を「updatenovel」に変更し、テキストを変更しますノードの子ノードを「ララララ」にします。

XmlNodeList nodeList=xmlDoc.SelectSingleNode("bookstore").ChildNodes;//获取bookstore节点的所有子节点 foreach(XmlNode xn in nodeList)//遍历所有子节点 { XmlElement xe=(XmlElement)xn;//将子节点类型转换为XmlElement类型 if(xe.GetAttribute("genre")=="novel")//如果genre属性值为“李赞红” { xe.SetAttribute("genre","updatenovel");//则修改该属性为“update李赞红” XmlNodeList nls=xe.ChildNodes;//继续获取xe子节点的所有子节点 foreach(XmlNode xn1 in nls)//遍历 { XmlElement xe2=(XmlElement)xn1;//转换类型 if(xe2.Name=="title")//如果找到 { xe2.InnerText="亚胜";//则修改 break;//找到退出来就可以了 } } break; } } 
xmlDoc.Save("bookstore.xml");//保存。

4.

ノードのジャンル属性を削除し、ノードを削除します。

5. XML をトラバース


以上がXmlDocument オブジェクト操作の詳細な手順の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。