Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home CSharp C# Parse Item Value And Name In XML String

C# Parse Item Value And Name In XML String

An ASP.net (written in C#) example below will show How to read item name and item value of each element in a XML string. In order to keep it simple, the XML string just contains 2 elements with text inside a CDATA section and have one attribute.

The asp.net example below will use XmlDocument to load the XML string so we need to use System.Xml namespace.

C# read item name, value and attribute from XML string

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
 
public partial class parse_item_value_from_xml_string : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string xml = "";
        xml += "<xml>";
        xml += "<first_name title='Mr'><![CDATA[Hoan]]></first_name>";
        xml += "<last_name><![CDATA[Huynh]]></last_name>";
        xml += "<website>http://4rapiddev.com</website>";
        xml += "<role>Admin</role>";
        xml += "<first_name title='Ms'><![CDATA[Chau]]></first_name>";
        xml += "<last_name><![CDATA[Nguyen]]></last_name>";
        xml += "<website>http://4rapiddev.com</website>";
        xml += "<role>Editor</role>";
        xml += "</xml>";
 
        XmlDocument dom = new XmlDocument();
        dom.LoadXml(xml);
        XmlNodeList root = dom.DocumentElement.ChildNodes;
 
        for (int i = 0; i < root.Count; i++)
        {
            XmlAttributeCollection attr = root.Item(i).Attributes;
 
            for (int j = 0; j < attr.Count; j++)
            {
                Response.Write(attr.Item(j).Name + ": " + attr.Item(j).InnerText + "<br>");
            }
            Response.Write(root.Item(i).Name + ": " + root.Item(i).InnerText + "<br>");
        }
    }
}

using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Xml; public partial class parse_item_value_from_xml_string : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string xml = ""; xml += "<xml>"; xml += "<first_name title='Mr'><![CDATA[Hoan]]></first_name>"; xml += "<last_name><![CDATA[Huynh]]></last_name>"; xml += "<website>http://4rapiddev.com</website>"; xml += "<role>Admin</role>"; xml += "<first_name title='Ms'><![CDATA[Chau]]></first_name>"; xml += "<last_name><![CDATA[Nguyen]]></last_name>"; xml += "<website>http://4rapiddev.com</website>"; xml += "<role>Editor</role>"; xml += "</xml>"; XmlDocument dom = new XmlDocument(); dom.LoadXml(xml); XmlNodeList root = dom.DocumentElement.ChildNodes; for (int i = 0; i < root.Count; i++) { XmlAttributeCollection attr = root.Item(i).Attributes; for (int j = 0; j < attr.Count; j++) { Response.Write(attr.Item(j).Name + ": " + attr.Item(j).InnerText + "<br>"); } Response.Write(root.Item(i).Name + ": " + root.Item(i).InnerText + "<br>"); } } }

Output

title: Mr
first_name: Hoan
last_name: Huynh
website: http://4rapiddev.com
role: Admin
title: Ms
first_name: Chau
last_name: Nguyen
website: http://4rapiddev.com
role: Editor

title: Mr first_name: Hoan last_name: Huynh website: http://4rapiddev.com role: Admin title: Ms first_name: Chau last_name: Nguyen website: http://4rapiddev.com role: Editor

Jan 15, 2012Hoan Huynh
Turn On 3G On iPhone 4sDisable Paste On Textbox With JavaScript
You Might Also Like:
  • Get or Find Facebook Profile Id Number
  • PHP Connect To MS SQL Server
  • PHP Connect To MySQL Read Data And Export XML Format
  • String To Lower Case In PHP, JavaScript And .Net (CSharp)
  • String To Upper Case In PHP, JavaScript And .Net (CSharp)
  • Replace String In MySQL
  • PHP Login Or Sign In With Google Account By OpenID
  • C# Get File Extension Without Sub String Or Split Function
  • ASP.NET Get Facebook URL Total Comments, Total Likes, Total Shares
  • PHP Get Likes Number Of Facebook Page
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

10 years ago CSharpChildNodes, loadXML, xml, XmlAttributeCollection, XmlDocument, XmlNodeList279
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
24,469 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
21,844 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
17,661 views
JQuery Allow only numeric characters or only alphabet characters in textbox
15,004 views
C# Read Json From URL And Parse/Deserialize Json
11,737 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Things to Learn about Installingderm Loan Type S
  • Online Photo Editor – Free Photoediting Software
  • A Guide to Finding the Best Paper Sellers
  • Photoediting in Home Isn’t Hard to Do!

  • Free Photo Editor Online
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (27)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (647)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development