Click here to Skip to main content
12,444,517 members (55,162 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# XML XmlReader
Hi



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>
                <Address>
                    <Address1>700 WATERDALE RD</Address1>
                    <Address2/>
                    <Suburb>BANDURA</Suburb>
                    <State>VIC</State>
                    &4t;Postcode>

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.MoveToContent();
  reader.ReadToDescendant("book");
  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);
           }
           Console.Write(">");
           break;
        case XmlNodeType.Text:
           Console.Write(reader.Value);
           break;
        case XmlNodeType.EndElement:
           Console.Write("</{0}>", reader.Name);
           break;
     }
  }  while (reader.Read());
 
}
Posted 25-Feb-13 1:13am
Rectus421
Updated 25-Feb-13 1:41am
v2
Comments
digimanus 25-Feb-13 8:09am
   
have you read this?

1 solution

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

Solution 1

Hi,
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
{
    [Serializable]
    [XmlRoot("RootElement")]
    public class RootElement
    {
        [XmlElement]
        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" 
                                                    };
 
            xml_serialise(testOut);
 
//============== 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;
        }
    }
}


Regards
Jegan
  Permalink  
v2

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 | Mobile
Web02 | 2.8.160811.3 | Last Updated 25 Feb 2013
Copyright © CodeProject, 1999-2016
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