Click here to Skip to main content
13,548,896 members
Rate this:
Please Sign up or sign in to vote.
See more:

I have Following XML File.... When I run, It Reads Only "Company Name" not All Parameters
I have to move on to descendants.

<CompanyName>RSJ TRADING PTY LTD T/AS BM GU</CompanyName>
                <BusinessName>RSJ TRADING PTY LTD T/AS BM GU</BusinessName>
                    <Address1>700 WATERDALE RD</Address1>

                <ContactName>JACQUELINE MAO</ContactName>

This is code under Button "Get Data"

XmlReader xmlRdr = XmlReader.Create(@"E:\Form Data.xml");
        while (xmlRdr.Read())
            if (xmlRdr.Name == "CompanyName")
                Label1.Text = xmlRdr.ReadString();
            if (xmlRdr.Name == "AccountNumber")
              Label2.Text = xmlRdr.ReadString();
            if (xmlRdr.Name == "Address1")
                Label3.Text = xmlRdr.ReadString();
            if (xmlRdr.Name == "Suburb")
                Label4.Text = xmlRdr.ReadString();
            if (xmlRdr.Name == "State")
            Label7.Text = xmlRdr.ReadString();
            if (xmlRdr.Name == "Postcode")
              Label8.Text = xmlRdr.ReadString();

How can link above code with following code so that it reads and display all parameters
using (XmlReader reader = XmlReader.Create("2books.xml")) {

  // Move the reader to the second book node.
  reader.Skip(); //Skip the first book.

  // Parse the file starting with the second book node.
  do {
     switch (reader.NodeType) {
        case XmlNodeType.Element:
           Console.Write("<{0}", reader.Name);
           while (reader.MoveToNextAttribute()) {
               Console.Write(" {0}='{1}'", reader.Name, reader.Value);
        case XmlNodeType.Text:
        case XmlNodeType.EndElement:
           Console.Write("</{0}>", reader.Name);
  }  while (reader.Read());

Posted 25-Feb-13 1:13am
Updated 25-Feb-13 1:41am
digimanus 25-Feb-13 8:09am
have you read this?

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

It is much easier to create an object and serialise and deserialise xml than manually reading and writing them.

Try the code below:

using System;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using System.Text;

namespace Test
    public class RootElement
        public int Id { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }

    public class Test
        public void setXmlValues()
//============= Write values to xml file ===============
            RootElement testOut = new RootElement {
                                                    Id = 1,
                                                    Name = "Customer Name",
                                                    Address = "Customer Address" 


//============== Read the values from xml ==========
            RootElement testIn = xml_deserialise();
            int id = testIn.Id;
            string Name = testIn.Name;
            string address = testIn.Address;


        private void xml_serialise(RootElement test)
            XmlSerializer ser = new XmlSerializer(typeof(RootElement));

            using (TextWriter writer = new StreamWriter("test.xml"))
                ser.Serialize(writer, test);

        private RootElement xml_deserialise()
            XmlSerializer ser = new XmlSerializer(typeof(RootElement));

            RootElement test;

            using (TextReader writer = new StreamReader("test.xml"))
                test = (RootElement)ser.Deserialize(writer);

            return test;


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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.180515.1 | Last Updated 25 Feb 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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