Click here to Skip to main content
14,602,559 members
Rate this:
Please Sign up or sign in to vote.
See more:
how can i retrieve a name of a xml child node into array list. i have a array list like:
<items>
  <item1 id="1"> Text </item1>
  <item2 id="2"> Text </item2>
</items>

I want ArrayList contain only child node name like: item1, item2. please help me in this..
Posted
Updated 10-Oct-11 20:32pm
v3
Comments
Ankur\m/ 11-Oct-11 2:31am
   
Why don't you put some effort, read about System.Xml namespace, and try the suggestions out.
You have been asking similar questions again. :doh:
Rate this:
Please Sign up or sign in to vote.

Solution 1

you can use XMLDocument or XMLReader class, let see the example of XMLDocuemnt

Arraylist ArrList = new Arraylist(); 
System.XML.XMLDocument objDom = new System.XML.XMLDocument();
objDom.load(//xml file path);
xmlNodeList XNList = objDom.getElementByTagName("items");
for each (xmlNode XN in XNList)
{
   for(int i=0; i< XN.childNodes.count; i++)
   {
      ArrayList.Add(XN.childNodes[i].Name);
   }
}
   
v2
Comments
Member 8214635 11-Oct-11 1:16am
   
it store a "items" in arraylist not a item1 & item2. please help in this.
koolprasadd 11-Oct-11 2:11am
   
i have update solution please check
nil_25_12 31-May-14 3:12am
   
how to do this fro multiple items
Rate this:
Please Sign up or sign in to vote.

Solution 2

string strCategoryId;
XmlDocument oXMLDoc = null;
XmlNodeList oXMLNodeList;

try
{
//..this is your connection method, Admin is class Name in which method is define

oXMLDoc = oAdmin.GetPartyDetails();
oXMLNodeList = oXMLDoc.GetElementsByTagName("ABC");
if ((oXMLDoc.ChildNodes.Count >= 1) && (oXMLNodeList.Count > 0))
{

cboParty.Items.Add(new ListItem("-- Select Category --", "0"));
for (int intLoop = 0; intLoop <= oXMLNodeList.Count - 1; intLoop++)
{
strCategoryId = oXMLDoc.GetElementsByTagName("ABC").Item(intLoop).InnerText;
cboParty.Items.Add(new ListItem(strCategoryId));
}
}
else
{

ABC.Items.Add(new ListItem("No Category, Please add."));
}
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

create, read and write xml file through c# asp .net

//default.cs file

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Xml_Insert();
            Xml_update();
        }
    }

    public void Xml_Insert()
    {
       
            XmlDocument xmlDoc = new XmlDocument();

            // Write down the XML declaration
            XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);

            // Create the root element
            XmlElement rootNode = xmlDoc.CreateElement("CategoryList");
            xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);
            xmlDoc.AppendChild(rootNode);

            // Create a new <Category> element and add it to the root node
            XmlElement parentNode = xmlDoc.CreateElement("Category");

            // Set attribute name and value!
            parentNode.SetAttribute("ID", "01");

            xmlDoc.DocumentElement.PrependChild(parentNode);

            // Create the required nodes
            XmlElement mainNode = xmlDoc.CreateElement("MainCategory");
            XmlElement descNode = xmlDoc.CreateElement("Description");
            XmlElement activeNode = xmlDoc.CreateElement("Active");

            // retrieve the text 
            XmlText categoryText = xmlDoc.CreateTextNode("XML");
            XmlText descText = xmlDoc.CreateTextNode("This is a list my XML articles.");
            XmlText activeText = xmlDoc.CreateTextNode("true");

            // append the nodes to the parentNode without the value
            parentNode.AppendChild(mainNode);
            parentNode.AppendChild(descNode);
            parentNode.AppendChild(activeNode);

            // save the value of the fields into the nodes
            mainNode.AppendChild(categoryText);
            descNode.AppendChild(descText);
            activeNode.AppendChild(activeText);

            // Save to the XML file
            xmlDoc.Save(Server.MapPath("categories.xml"));

            Response.Write("XML file created");
       
    }
    public void Xml_update()
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(Server.MapPath("categories.xml"));

        XmlNodeList nodeList = xmlDoc.SelectNodes("/CategoryList/Category[@ID='01']");
        nodeList[0].ChildNodes[0].InnerText = "Deepak";
        nodeList[0].ChildNodes[1].InnerText = "Pooja";
        nodeList[0].ChildNodes[2].InnerText = "Ashok";

        // Don't forget to save the file
        xmlDoc.Save(Server.MapPath("categories.xml"));
        Response.Write("XML File updated!");
    }
}


//default.aspx

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    </div>
    </form>
</body>
</html>






// xml file

<?xml version="1.0" encoding="utf-8"?>
<CategoryList>
  <Category ID="01">
    <MainCategory>Deepak</MainCategory>
    <Description>Pooja</Description>
    <Active>Ashok</Active>
  </Category>
</CategoryList>
   
Comments
Member 8214635 11-Oct-11 1:44am
   
i dont understand

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100