protected void btnMidterm_Click(object sender, EventArgs e) { XPathNavigator nav; XPathDocument docNav; XPathNodeIterator NodeIter; //open the xml file String rootPath = Server.MapPath("~"); string strFilename = rootPath + "\\catalog.xml"; docNav = new XPathDocument(strFilename); //create navigator with xml document nav = docNav.CreateNavigator(); NodeIter = nav.Select("//catalog_item[price<40]"); while (NodeIter.MoveNext()) { XPathNodeIterator firstText = NodeIter.Current.SelectChildren(XPathNodeType.Element); txtMidterm3a.Text += firstText.Current.SelectSingleNode("price") + Environment.NewLine; txtMidterm3a.Text += firstText.Current.GetAttribute("gender", "") + Environment.NewLine; txtMidterm3a.Text += Environment.NewLine; } NodeIter = nav.Select("//catalog_item/size[@description='Small']/../item_number"); while (NodeIter.MoveNext()) { txtMidterm3b.Text += NodeIter.Current; txtMidterm3b.Text += Environment.NewLine; } }
protected void btnReturnOnlyMales_Click(object sender, EventArgs e) { XPathNavigator nav; XPathDocument docNav; XPathNodeIterator NodeIter; // Open the XML. String rootPath = Server.MapPath("~"); string strFilename = rootPath + "\\ContactManager.xml"; docNav = new XPathDocument(strFilename); // Create a navigator to query with XPath. nav = docNav.CreateNavigator(); // Select the node and place the results in an iterator. NodeIter = nav.Select("/ContactManager/Contact[@Sex='M']"); while (NodeIter.MoveNext()) { XPathNavigator ContactInformation = NodeIter.Current; txtResults3.Text = txtResults3.Text + ContactInformation.SelectSingleNode("FirstName").ToString() + " "; txtResults3.Text = txtResults3.Text + ContactInformation.SelectSingleNode("LastName").ToString() + Environment.NewLine; txtResults3.Text = txtResults3.Text + ContactInformation.SelectSingleNode("PhoneNumber").ToString() + Environment.NewLine; txtResults3.Text = txtResults3.Text + ContactInformation.SelectSingleNode("FaxNumber").ToString() + Environment.NewLine; txtResults3.Text = txtResults3.Text + ContactInformation.SelectSingleNode("EmailAddress").ToString() + Environment.NewLine; txtResults3.Text = txtResults3.Text + Environment.NewLine + "------" + Environment.NewLine; } }
public static async Task EditXmlDocumentAsync(string path) { XPathNodeIterator NodeIter; string strExpression; XmlDocument editableDocument = new XmlDocument(); // can edit editableDocument.Load(path); XPathNavigator editableNavigator = editableDocument.CreateNavigator(); editableNavigator.SelectSingleNode("Students/Student[@id='1007']/Name").SetValue("Nhật Bình"); await Task.Run(() => editableDocument.Save(path)); strExpression = "/Students/Student"; NodeIter = editableNavigator.Select(strExpression); Console.WriteLine("List of students(after edit):"); //Iterate through the results showing the element value. while (NodeIter.MoveNext()) { Console.WriteLine("Student name: {0}", NodeIter.Current.Value); } ; }
private void btnGetNodes_Click(object sender, EventArgs e) { XPathNavigator nav; XPathDocument docNav; XPathNodeIterator NodeIter; // Open the XML. string xmlFile = Application.StartupPath + "\\books.xml"; docNav = new XPathDocument(xmlFile); // Create a navigator to query with XPath. nav = docNav.CreateNavigator(); // Select the node and place the results in an iterator. NodeIter = nav.Select(txtNodeList.Text); cmbNodes.Items.Clear(); //Iterate through the results showing the element value. while (NodeIter.MoveNext()) { cmbNodes.Items.Add(NodeIter.Current.Value); // if you use /bookstore/book, then child XPathNodeIterator KidNodeIter; KidNodeIter = NodeIter.Current.Select("first-name"); while (KidNodeIter.MoveNext()) { cmbNodes.Items.Add(KidNodeIter.Current.Value); } } ; }
public string[] cityAutoSuggestion(string prefixText, int count) { XPathNavigator nav; XPathDocument docNav; XPathNodeIterator NodeIter; string Strexpression; int i = 0; string[] results = new string[count]; docNav = new XPathDocument(@"c:\books.xml"); nav = docNav.CreateNavigator(); Strexpression = "/bookstore/book/title[../price>10.00]"; Strexpression = "//author"; NodeIter = nav.Select(Strexpression); do { i++; NodeIter.MoveNext(); //Console.WriteLine("Book Title: {0}", NodeIter.Current.Value); results.SetValue(NodeIter.Current.Value, i - 1); } while(i < count); return(results); }
public Billing GetBillingAddressForAnOrder(int OrderID) { XPathNavigator nav; XPathDocument docNav; XPathNodeIterator NodeIter; // Open the XML. string directory = HttpContext.Current.Server.MapPath("."); string strFilename = directory + "\\" + "OrderInfoLab3.xml"; docNav = new XPathDocument(strFilename); // Create a navigator to query with XPath. nav = docNav.CreateNavigator(); NodeIter = nav.Select("//Order[@id=\"" + OrderID + "\"]/BillingInformation"); Billing myBilling = new Billing(); while (NodeIter.MoveNext()) { myBilling.Name = NodeIter.Current.SelectSingleNode("Name").Value; myBilling.Address = NodeIter.Current.SelectSingleNode("Address").Value; myBilling.City = NodeIter.Current.SelectSingleNode("City").Value; myBilling.State = NodeIter.Current.SelectSingleNode("State").Value; myBilling.ZipCode = NodeIter.Current.SelectSingleNode("ZipCode").Value; } return(myBilling); }
private void loadNrlDataSets(string filepath, TreeView treeView) { try { XPathNavigator nav; XPathDocument docNav; XPathNodeIterator NodeIter; // Open the XML. docNav = new XPathDocument(filepath); // Create a navigator to query with XPath. nav = docNav.CreateNavigator(); NodeIter = nav.Select("/NRL_DataSet_List/NRL_DataSet"); while (NodeIter.MoveNext()) { treeView.Nodes.Add(getTreeNodeFromNrlDataSet(NodeIter)); } } catch (Exception ex) { Log.Write(ex); } }
private void btnLastName_Click(object sender, EventArgs e) { rtLastName.Text = ""; XPathNavigator nav; XPathDocument docNav; XPathNodeIterator NodeIter; // Open the XML. string xmlFile = Application.StartupPath + "\\ContactXML.xml"; docNav = new XPathDocument(xmlFile); // Create a navigator to query with XPath. nav = docNav.CreateNavigator(); // Select the node and place the results in an iterator. String searchString = ""; searchString = "/Contacts/Contact[LastName='" + txtLastName.Text + "']"; // Select the node and place the results in an iterator. NodeIter = nav.Select(searchString); //Iterate through the results showing the element value. while (NodeIter.MoveNext()) { rtLastName.Text += NodeIter.Current.Value + Environment.NewLine; } }
static void Main(string[] args) { XPathNavigator nav; XPathDocument docNav; XPathNodeIterator NodeIter; String query; // Load the XML. docNav = new XPathDocument(@"C:\Users\Administrator\Source\Repos\XPathDemo\XPath\Flights.xml"); // Create a navigator to query with XPath. nav = docNav.CreateNavigator(); query = "/Flights/Flight/Origin[text()='LHR']"; NodeIter = nav.Select(query); //Iterate through the results showing the element value. while (NodeIter.MoveNext()) { Console.WriteLine(NodeIter.Current.OuterXml); // Console.WriteLine("Airline: {0}", NodeIter.Current.Value); } ; Console.ReadLine(); }
private void btnReturnAll_Click(object sender, EventArgs e) { rtAllInfo.Text = ""; XPathNavigator nav; XPathDocument docNav; XPathNodeIterator NodeIter; // Open the XML. string xmlFile = Application.StartupPath + "\\ContactXML.xml"; docNav = new XPathDocument(xmlFile); // Create a navigator to query with XPath. nav = docNav.CreateNavigator(); lblVersionDate.Text = "Version Date - " + nav.SelectSingleNode("/Contacts/LatestVersion").Value; lblVersionNumber.Text = "Version Number - " + nav.SelectSingleNode("/Contacts/CurrentVersion").Value; // Select the node and place the results in an iterator. NodeIter = nav.Select("/Contacts/Contact/*"); while (NodeIter.MoveNext()) { rtAllInfo.Text += NodeIter.Current.Value + Environment.NewLine; } }
//----------------------------------------------------------------------------------- // Read the Customers //----------------------------------------------------------------------------------- protected List <Customer> readCustomers(XPathNavigator nav) { XPathNodeIterator NodeIter; List <Customer> list = new List <Customer>(); NodeIter = nav.Select("/project/customer"); while (NodeIter.MoveNext()) { Customer c = new Customer(); c.name = NodeIter.Current.SelectSingleNode("name").Value; c.logoUrl = NodeIter.Current.SelectSingleNode("logoUrl").Value; c.shortDescription = NodeIter.Current.SelectSingleNode("shortDescription").Value; XPathNodeIterator userIter = NodeIter.Current.Select("users/user"); while (userIter.MoveNext()) { c.users.Add(userIter.Current.InnerXml); } XPathNodeIterator partyIter = NodeIter.Current.Select("interstedParties/party"); while (partyIter.MoveNext()) { c.interstedParties.Add(partyIter.Current.InnerXml); } list.Add(c); } return(list); }
private void LoadReportMenu() { XPathNodeIterator NodeIter; #region alternate way of reading the malformed xml string xmlPath = Server.MapPath(ApplicationConstants.REPORTING_MENU_FILE); System.IO.StreamReader sRead = new System.IO.StreamReader(xmlPath); string line = string.Empty; string fullXmlFile = string.Empty; string navigateUrl = "NavigateUrl"; string xmlQuote = "\""; while (!String.IsNullOrEmpty(line = sRead.ReadLine())) { if (!line.Contains(navigateUrl)) { fullXmlFile += line; } else { string badUrl = line.Substring(line.IndexOf(navigateUrl) + navigateUrl.Length, line.Length - line.IndexOf(navigateUrl) - navigateUrl.Length); //excluding "good" xml quotes int xmlQuotePosition = badUrl.IndexOf(xmlQuote); badUrl = badUrl.Substring(xmlQuotePosition + 1, badUrl.Length - (xmlQuotePosition + 1)); badUrl = badUrl.Substring(0, badUrl.LastIndexOf(xmlQuote)); string goodUrl = System.Security.SecurityElement.Escape(badUrl); fullXmlFile += line.Replace(badUrl, goodUrl); } } sRead.Close(); System.IO.StringReader stringRead = new System.IO.StringReader(fullXmlFile); XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(stringRead); stringRead.Close(); XPathDocument xmlFile = new XPathDocument(new XmlNodeReader(xmlDocument)); #endregion reading xml //XPathDocument xmlFile = new XPathDocument(Server.MapPath(ApplicationConstants.REPORTING_MENU_FILE)); XPathNavigator xmlNavigator = xmlFile.CreateNavigator(); int nodeCounter = 0; //Initial XPathNavigator to start at the root. xmlNavigator.MoveToRoot(); NodeIter = xmlNavigator.Select(ApplicationConstants.REPORTING_MENU_ITEM); while (NodeIter.MoveNext()) { levelCount = 1; string cssClass = getCssClass(NodeIter.Count, nodeCounter); AddItemsToMenu(NodeIter.Current, "0", cssClass); nodeCounter++; repBreadCrumb = "Reporting"; } }
public Project readProject(XPathNavigator nav) { XPathNodeIterator NodeIter; Project p = new Project(); p.name = nav.SelectSingleNode("/project/name").Value; p.type = nav.SelectSingleNode("/project/type").Value; NodeIter = nav.Select("/project/tags/tag"); while (NodeIter.MoveNext()) { p.tags.Add(NodeIter.Current.InnerXml); } ; p.faculty = nav.SelectSingleNode("/project/faculty").Value; p.year = Convert.ToInt16(nav.SelectSingleNode("/project/year").Value); p.shortDescription = nav.SelectSingleNode("/project/shortDescription").Value; try { p.projectImageUrl = nav.SelectSingleNode("/project/projectImageUrl").Value; } catch { p.projectImageUrl = ""; } p.groupCode = nav.SelectSingleNode("project/students/groupCode").Value; p.projectLandingPage = nav.SelectSingleNode("/project/projectLandingPage").Value; p.movie = nav.SelectSingleNode("/project/movie").Value; NodeIter = nav.Select("/project/advisors/advisor"); while (NodeIter.MoveNext()) { p.advisors.Add(NodeIter.Current.InnerXml); } NodeIter = nav.Select("/project/technologies/technology"); while (NodeIter.MoveNext()) { p.technologies.Add(NodeIter.Current.GetAttribute("name", "")); } p.challenges = nav.SelectSingleNode("/project/challenges").Value; p.personalNote = nav.SelectSingleNode("/project/personalNote").Value; p.modules = readModules(nav); p.customers = readCustomers(nav); p.students = readStudents(nav); p.screenshots = readScreenshots(nav); p.goals = readGoals(nav); return(p); }
private void button2_Click(object sender, EventArgs e) { richTextBox1.Clear(); XPathDocument docNav; XPathNavigator nav; XPathNodeIterator NodeIter; if (textBox1.Text.Equals("") || textBox2.Text.Equals("")) { if (textBox1.Text.Equals("")) { MessageBox.Show(EXpathInfo); } if (textBox2.Text.Equals("")) { MessageBox.Show(EFilePathInfo); } } else { docNav = new XPathDocument(textBox2.Text); nav = docNav.CreateNavigator(); NodeIter = nav.Select(textBox1.Text); while (NodeIter.MoveNext()) { if (NodeIter.Current != null) { bool exclude = false; foreach (string excludeItem in listBox1.Items) { if (NodeIter.Current.InnerXml.Equals(excludeItem)) { exclude = true; break; } } if (!exclude) { richTextBox1.AppendText(NodeIter.Current.InnerXml + "\r\n"); richTextBox1.AppendText("------------------\r\n"); } } } } }
//----------------------------------------------------------------------------------- // Read the Students //----------------------------------------------------------------------------------- protected List <Student> readStudents(XPathNavigator nav) { List <Student> list = new List <Student>(); XPathNodeIterator NodeIter; NodeIter = nav.Select("/project/students/student"); while (NodeIter.MoveNext()) { Student s = new Student(); s.name = NodeIter.Current.SelectSingleNode("name").Value; s.imageUrl = NodeIter.Current.SelectSingleNode("imageUrl").Value; list.Add(s); } return(list); }
//----------------------------------------------------------------------------------- // Read the ScreenShots //----------------------------------------------------------------------------------- protected List <ScreenShot> readScreenshots(XPathNavigator nav) { List <ScreenShot> list = new List <ScreenShot>(); XPathNodeIterator NodeIter; NodeIter = nav.Select("/project/screenShots/screenShot"); while (NodeIter.MoveNext()) { ScreenShot s = new ScreenShot(); s.description = NodeIter.Current.SelectSingleNode("description").Value; s.imageUrl = NodeIter.Current.SelectSingleNode("imageUrl").Value; list.Add(s); } return(list); }
//----------------------------------------------------------------------------------- // Read the goals //----------------------------------------------------------------------------------- protected List <Goal> readGoals(XPathNavigator nav) { List <Goal> list = new List <Goal>(); XPathNodeIterator NodeIter; NodeIter = nav.Select("/project/goals/goal"); while (NodeIter.MoveNext()) { Goal g = new Goal(); g.description = NodeIter.Current.SelectSingleNode("description").Value; g.status = NodeIter.Current.SelectSingleNode("status").Value; list.Add(g); } return(list); }
private void buttonApply_Click(object sender, EventArgs e) { XPathNavigator nav; XPathDocument docNav; XPathNodeIterator NodeIter; try { // Open the XML. MemoryStream m = new MemoryStream(System.Text.Encoding.Default.GetBytes(XMLString)); docNav = new XPathDocument(m); // Create a navigator to query with XPath. nav = docNav.CreateNavigator(); XmlNamespaceManager manager = new XmlNamespaceManager(nav.NameTable); string[] xPathExpression = textExpression.Text.Split(' '); for (int i = 1; i < xPathExpression.Length; i++) { string[] nameSpace = xPathExpression[i].Split('='); if (nameSpace.Length < 2) { manager.AddNamespace(String.Empty, nameSpace[0]); } else { manager.AddNamespace(nameSpace[0], nameSpace[1]); } } NodeIter = nav.Select(xPathExpression[0], manager); textResult.Clear(); while (NodeIter.MoveNext()) { textResult.Text += NodeIter.Current.OuterXml + Environment.NewLine; } ; } catch (Exception ex) { this.ShowError(ex); } }
//----------------------------------------------------------------------------------- // Read the Moduls //----------------------------------------------------------------------------------- protected List <Module> readModules(XPathNavigator nav) { List <Module> list = new List <Module>(); XPathNodeIterator NodeIter; NodeIter = nav.Select("/project/modules/module"); while (NodeIter.MoveNext()) { Module m = new Module(); m.name = NodeIter.Current.SelectSingleNode("name").Value; m.description = NodeIter.Current.SelectSingleNode("description").Value; XPathNodeIterator featureIter = NodeIter.Current.Select("features/feature"); while (featureIter.MoveNext()) { m.features.Add(featureIter.Current.InnerXml); } list.Add(m); } return(list); }
public List <Product> readProducts(XPathNavigator nav) { List <Product> list = new List <Product>(); XPathNodeIterator NodeIter; NodeIter = nav.Select("/Products/Product"); while (NodeIter.MoveNext()) { Product p = new Product(); p.ProductId = Convert.ToInt32(NodeIter.Current.GetAttribute("product_id", "")); p.Title = NodeIter.Current.GetAttribute("title", ""); p.ImagePath = NodeIter.Current.GetAttribute("img_url", ""); p.Inventory = Convert.ToInt32(NodeIter.Current.GetAttribute("inventory", "")); p.CategoryId = Convert.ToInt32(NodeIter.Current.GetAttribute("category_id", "")); p.Active = NodeIter.Current.GetAttribute("active", ""); p.Price = Convert.ToInt32(NodeIter.Current.GetAttribute("price", "")); list.Add(p); } return(list); }
public List <Order> GetItemListForOrder(int OrderID) { XPathNavigator nav; XPathDocument docNav; XPathNodeIterator NodeIter; List <Order> itemOrder = new List <Order>(); // Open the XML. string directory = HttpContext.Current.Server.MapPath("."); string strFilename = directory + "\\" + "OrderInfoLab3.xml"; docNav = new XPathDocument(strFilename); // Create a navigator to query with XPath. nav = docNav.CreateNavigator(); NodeIter = nav.Select("//Order[@id=\"" + OrderID + "\"]/Items/Item"); while (NodeIter.MoveNext()) { Order myOrder = new Order(); myOrder.PartNo = NodeIter.Current.SelectSingleNode("PartNo").Value; myOrder.Description = NodeIter.Current.SelectSingleNode("Description").Value; myOrder.UnitPrice = Convert.ToDouble(NodeIter.Current.SelectSingleNode("UnitPrice").Value); myOrder.Quantity = Convert.ToInt32(NodeIter.Current.SelectSingleNode("Quantity").Value); if (NodeIter.Current.SelectSingleNode("CustomerOptions").Value.Equals("")) { myOrder.Size = null; myOrder.Color = null; } else { myOrder.Size = NodeIter.Current.SelectSingleNode("//CustomerOptions/Size").Value; myOrder.Color = NodeIter.Current.SelectSingleNode("//CustomerOptions/Color").Value; } itemOrder.Add(myOrder); } return(itemOrder); }
//Shipping info private void btnShipping_Click(object sender, EventArgs e) { rtShipping.Text = ""; XPathNavigator nav; XPathDocument docNav; XPathNodeIterator NodeIter; // Open the XML. string xmlFile = Application.StartupPath + "\\OrderInfo.xml"; docNav = new XPathDocument(xmlFile); // Create a navigator to query with XPath. nav = docNav.CreateNavigator(); // Select the node and place the results in an iterator. NodeIter = nav.Select("//Order/ShippingInformation/*"); while (NodeIter.MoveNext()) { rtShipping.Text += NodeIter.Current.Value + Environment.NewLine; } }
private void ParseXml(string strXml, WFSCapabilities wfsCapabilities) { XPathNavigator nav; XPathDocument docNav; XPathNodeIterator NodeIter; // String strExpression; try { using (StringReader stream = new StringReader(strXml)) { docNav = new XPathDocument(stream); nav = docNav.CreateNavigator(); var ns = new XmlNamespaceManager(nav.NameTable); ns.AddNamespace("wfs", "http://www.opengis.net/wfs"); ns.AddNamespace("ows", "http://www.opengis.net/ows"); ns.AddNamespace("xlink", "http://www.w3.org/1999/xlink"); XPathExpression expr = nav.Compile("/wfs:WFS_Capabilities/wfs:Capability/wfs:Request/wfs:GetFeature/wfs:ResultFormat/*"); expr.SetContext(ns); NodeIter = nav.Select(expr); var formats = new List <string>(); while (NodeIter.MoveNext()) { if (!string.IsNullOrEmpty(NodeIter.Current.Name)) { formats.Add(NodeIter.Current.Name); } } wfsCapabilities.Capability.Requests.GetFeaturesRequest.Formats = formats; } } catch (Exception) { } }
//Item info private void btnItems_Click(object sender, EventArgs e) { rtItem.Text = ""; XPathNavigator nav; XPathDocument docNav; XPathNodeIterator NodeIter; // Open the XML. string xmlFile = Application.StartupPath + "\\OrderInfo.xml"; docNav = new XPathDocument(xmlFile); // Create a navigator to query with XPath. nav = docNav.CreateNavigator(); //Calculates the Count of Items lblItemsBought.Text = "Total Items Bought: " + nav.Evaluate("count(//Order/Items/Item)"); //Calculates the Total Cost of Items lblCost.Text = "Total Cost: " + nav.Evaluate("sum(//Order/Items/Item/TotalCost)"); // Select the node and place the results in an iterator. NodeIter = nav.Select("//Order/Items/Item/*"); while (NodeIter.MoveNext()) { if (NodeIter.Current.Value.Equals("")) { rtItem.Text += "No Customer Options" + Environment.NewLine; } else { rtItem.Text += NodeIter.Current.Value + Environment.NewLine; } } }
protected void btnGetResults_Click(object sender, EventArgs e) { string xml_query = ""; txtShowResults.Text = ""; XPathNavigator nav; XPathDocument docNav; XPathNodeIterator NodeIter; String rootPath = Server.MapPath("~"); string strFilename = rootPath + "\\Nutrition.xml"; docNav = new XPathDocument(strFilename); nav = docNav.CreateNavigator(); xml_query = "//food[vitamins/a > 10]"; NodeIter = nav.Select(xml_query); txtShowResults.Text = "****Food that has more than 10 of Vitamin A**** " + Environment.NewLine; while (NodeIter.MoveNext()) { txtShowResults.Text += "Name: " + NodeIter.Current.SelectSingleNode("name").Value + Environment.NewLine; txtShowResults.Text += "Total Calories: " + NodeIter.Current.SelectSingleNode("calories/@total").Value + Environment.NewLine; txtShowResults.Text += "............................." + Environment.NewLine; } txtShowResults.Text += "==================================" + Environment.NewLine; txtShowResults.Text += "****Food that has a meat type**** " + Environment.NewLine; xml_query = "//food[@type = 'meat']"; NodeIter = nav.Select(xml_query); while (NodeIter.MoveNext()) { txtShowResults.Text += "Name: " + NodeIter.Current.SelectSingleNode("name").Value + Environment.NewLine; } }
protected void btnGetInformationAnotherWay_Click(object sender, EventArgs e) { XPathNavigator nav; XPathDocument docNav; XPathNodeIterator NodeIter; // Open the XML. String rootPath = Server.MapPath("~"); string strFilename = rootPath + "\\ContactManager.xml"; docNav = new XPathDocument(strFilename); // Create a navigator to query with XPath. nav = docNav.CreateNavigator(); // Select the node and place the results in an iterator. NodeIter = nav.Select("/ContactManager/Contact/*"); while (NodeIter.MoveNext()) { txtResults2.Text = txtResults2.Text + NodeIter.Current.Value + Environment.NewLine; } }
protected void btnGetInformation_Click(object sender, EventArgs e) { XPathNavigator nav; XPathDocument docNav; XPathNodeIterator NodeIter; // Open the XML. String rootPath = Server.MapPath("~"); string strFilename = rootPath + "\\ContactManager.xml"; docNav = new XPathDocument(strFilename); // Create a navigator to query with XPath. nav = docNav.CreateNavigator(); lblVersionDate.Text = "Version Date - " + nav.SelectSingleNode("/ContactManager/VersionDate").Value; lblVersionNumber.Text = "Version Number - " + nav.SelectSingleNode("/ContactManager/VersionNumber").Value; //// Select the node and place the results in an iterator. NodeIter = nav.Select("/ContactManager/Contact"); while (NodeIter.MoveNext()) { XPathNodeIterator contactInfo = NodeIter.Current.SelectChildren(XPathNodeType.Element); txtResults1.Text = txtResults1.Text + " First Name: " + contactInfo.Current.SelectSingleNode("FirstName") + Environment.NewLine; txtResults1.Text = txtResults1.Text + contactInfo.Current.SelectSingleNode("LastName") + Environment.NewLine; txtResults1.Text = txtResults1.Text + contactInfo.Current.SelectSingleNode("PhoneNumber") + Environment.NewLine; txtResults1.Text = txtResults1.Text + contactInfo.Current.SelectSingleNode("FaxNumber") + Environment.NewLine; txtResults1.Text = txtResults1.Text + contactInfo.Current.SelectSingleNode("EmailAddress") + Environment.NewLine; if (contactInfo.Current.HasAttributes) { txtResults1.Text = txtResults1.Text + contactInfo.Current.GetAttribute("Sex", "") + Environment.NewLine; } txtResults1.Text = txtResults1.Text + Environment.NewLine + "-----" + Environment.NewLine; } }
// From: http://support.microsoft.com/kb/308333 /// <summary> /// Test Xml file handling /// </summary> static void TestXpathFile() { Console.WriteLine("== Test xpath with file"); XPathNavigator nav; XPathDocument docNav; XPathNodeIterator NodeIter; String strExpression; // Open the XML. docNav = new XPathDocument(@"d:\books1.xml"); // Create a navigator to query with XPath. nav = docNav.CreateNavigator(); // Find the average cost of a book. // This expression uses standard XPath syntax. strExpression = "sum(/bookstore/book/price) div count(/bookstore/book/price)"; // Use the Evaluate method to return the evaluated expression. Console.WriteLine("The average cost of the books are {0}", nav.Evaluate(strExpression)); // Find the title of the books that are greater then $10.00. strExpression = "/bookstore/book/title[../price>10.00]"; // Select the node and place the results in an iterator. NodeIter = nav.Select(strExpression); Console.WriteLine("List of expensive books:"); //Iterate through the results showing the element value. while (NodeIter.MoveNext()) { Console.WriteLine("Book Title: {0}", NodeIter.Current.Value); } ; }
private void btnReturnMales_Click(object sender, EventArgs e) { rtMales.Text = ""; XPathNavigator nav; XPathDocument docNav; XPathNodeIterator NodeIter; // Open the XML. string xmlFile = Application.StartupPath + "\\ContactXML.xml"; docNav = new XPathDocument(xmlFile); // Create a navigator to query with XPath. nav = docNav.CreateNavigator(); // Select the node and place the results in an iterator. NodeIter = nav.Select("/Contacts/Contact[@sex='M']"); while (NodeIter.MoveNext()) { rtMales.Text += NodeIter.Current.Value + Environment.NewLine; } }
protected void Page_Load(object sender, EventArgs e) { XPathNavigator nav; XPathDocument docNav; XPathNodeIterator NodeIter; //open the xml file String rootPath = Server.MapPath("~"); string strFilename = rootPath + "\\OrderInfo.xml"; docNav = new XPathDocument(strFilename); //create navigator with xml document nav = docNav.CreateNavigator(); lblNumber.Text += nav.Evaluate("count(/Order/Items/Item)").ToString(); lblTotal.Text += nav.Evaluate("sum(/Order/Items/Item/TotalCost)").ToString(); //select billing node and place results in iterator txtBilling.Text = ""; NodeIter = nav.Select("/Order/BillingInformation"); while (NodeIter.MoveNext()) { XPathNodeIterator billingInfo = NodeIter.Current.SelectChildren(XPathNodeType.Element); txtBilling.Text += "Name: " + billingInfo.Current.SelectSingleNode("Name") + Environment.NewLine; txtBilling.Text += "Address: " + billingInfo.Current.SelectSingleNode("Address") + Environment.NewLine; txtBilling.Text += "City: " + billingInfo.Current.SelectSingleNode("City") + Environment.NewLine; txtBilling.Text += "State: " + billingInfo.Current.SelectSingleNode("State") + Environment.NewLine; txtBilling.Text += "Zip Code: " + billingInfo.Current.SelectSingleNode("ZipCode") + Environment.NewLine; } //select shipping node and place results in iterator txtShipping.Text = ""; NodeIter = nav.Select("/Order/ShippingInformation"); while (NodeIter.MoveNext()) { XPathNodeIterator shippingInfo = NodeIter.Current.SelectChildren(XPathNodeType.Element); txtShipping.Text += "Name: " + shippingInfo.Current.SelectSingleNode("Name") + Environment.NewLine; txtShipping.Text += "Address: " + shippingInfo.Current.SelectSingleNode("Address") + Environment.NewLine; txtShipping.Text += "City: " + shippingInfo.Current.SelectSingleNode("City") + Environment.NewLine; txtShipping.Text += "State: " + shippingInfo.Current.SelectSingleNode("State") + Environment.NewLine; txtShipping.Text += "Zip Code: " + shippingInfo.Current.SelectSingleNode("ZipCode") + Environment.NewLine; } //select items node and place results in iterator txtItem.Text = ""; NodeIter = nav.Select("/Order/Items"); while (NodeIter.MoveNext()) { XPathNodeIterator item; item = NodeIter.Current.Select("Item"); while (item.MoveNext()) { XPathNodeIterator itemInfo = item.Current.SelectChildren(XPathNodeType.Element); txtItem.Text += "Part Number: " + itemInfo.Current.SelectSingleNode("PartNo") + Environment.NewLine; txtItem.Text += "Description: " + itemInfo.Current.SelectSingleNode("Description") + Environment.NewLine; txtItem.Text += "Unit Price: $" + itemInfo.Current.SelectSingleNode("UnitPrice") + Environment.NewLine; txtItem.Text += "Quantity: " + itemInfo.Current.SelectSingleNode("Quantity") + Environment.NewLine; txtItem.Text += "Total Cost: $" + itemInfo.Current.SelectSingleNode("TotalCost") + Environment.NewLine; if (itemInfo.Current.SelectSingleNode("CustomerOptions").HasChildren) { txtItem.Text += "Size: " + itemInfo.Current.SelectSingleNode("CustomerOptions/Size") + Environment.NewLine; txtItem.Text += "Color: " + itemInfo.Current.SelectSingleNode("CustomerOptions/Color") + Environment.NewLine; } txtItem.Text += Environment.NewLine + "--------------" + Environment.NewLine; } } }