XmlDocument doc = new XmlDocument(); //読み込むXMLを読み込む doc.Load(@ "F:\ Books.xml"); //ルートノードを取得する XmlElement books = doc.DocumentElement; //子ノードの戻りノードを取得する設定され ているXmlNodeList XNL = books.ChildNodes; foreachの(たXmlNode XNLの項目を) { 商品XE =のXmlElement(のXmlElement); Console.WriteLineを(xe.GetAttribute( "ID")); ているXmlNodeList nodelistと= xe.ChildNodes; foreachの(のXmlNode ITEM2 in nodeList) { Console.WriteLine(item2.InnerText); } } Console.ReadKey();
コンテンツを編集する
XmlDocument doc = new XmlDocument(); doc.Load(@ " F:\ Books.xml " ); XmlNodeList nodeList = doc.SelectSingleNode(" / Books / Book [@ id = '3d310e87-6c46-4874-859e-c09f3acce589' ] " ).ChildNodes; foreach(XmlNode xn in nodeList)// すべての子ノードをトラバースする { XmlElement xe =(XmlElement)xn; // 子ノードタイプをXmlElementタイプに変換する // Console.WriteLine(xe.GetAttribute(" id ")); if(xe.Name == " Price " ) { Console.WriteLine(xe.InnerText); xe.InnerText =" oooooooo " ; } } doc.Save(@ " F:\ Books.xml ");
属性を変更する
// プロパティの変更 XmlDocument doc = new XmlDocument(); doc.Load(" Order.xml " ); XmlNode xn = doc.SelectSingleNode(" / Order / Items / OrderItem [@ Name = '190'] " ); xn.Attributes [ " Count " ] .Value = " 200 " ; xn.Attributes [ " Name " ] .Value = " 颜世伟" ; doc.Save(" Order.xml " ); Console.WriteLine(" 保存成功"