vb.net - Cannot get the specific XML node -
I want to update the XML node and searched this site for example. Although I have not been set on an object reference for an object instance. Will someone show me how to get a node thanks in advance
This is my vb code:
Import System.Xml Import System.IO partial class test2 inherited System Web.UI.Page protected sub Page_Load (ByVal string as the object, as ByVal e System.EventArgs) handles as Me.Load dim xmlFileNamae string = "Vancouver.xml" dim xmlFilePath = ConfigurationManager.AppSettings ( " XMLFolder ") & amp; xmlFileNamae so File.Exists (xmlFilePath) then dim docXML as XmlDocument = new XmlDocument docXML.Load (xmlFilePath) Dim ID string = "1" Dim node XmlNode = docXML.SelectSingleNode (as as "/ Order / Order [@ id = ' "& amp; ID & amp;"'] / date ") node.InnerText = Date.Now node = docXML.SelectSingleNode (" / order / order [@ id = '' & amp; ID & amp; " '] / country ") node. innerText =" Vancouver "docXML.Save (xmlFilePath) end if end Sub end class My xml file:
& Lt; XML version = "1.0" encoding = "UTF-8"? & Gt; Order & gt; & lt; order id = "2" & gt; & lt; i M> organ -
7/24/2014 3:50:42 pm & lt; country & gt; china & lt; / Country & gt; & lt; / order & gt; & lt; order id = "1" & gt; Item & gt; Apple & lt; / item & gt; & lt; date & gt; 7 / 24/2014 3:50:42 pm & lt; / date & gt; & lt; Country & gt; China & lt; / country & gt; & lt; / order & gt; & lt; / order & gt; First of all, your XML is broken - you close the order first with , which is incorrect.
Secondly, your XPath is not good either.
There are XPaths with both a "cleaner" code. as XmlDoc as X XmlDoc XmlNode XmlDoc.Load (// Your Path) // This equals to order on first orders before / / where the ID attribute 1. node = XmlDoc.SelectSingleNode ( // do somehintg // it will give you the country of the first order items in order // where the id attribute is equal to 1. node = xmldoc.SelectSingleNode / order / command [@ id = "1"] / date ") // ("/ Order / order [@ id =" 1 "] / country") // Do something else
Comments
Post a Comment