Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#XMLXmlReader
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
Edited 25-Feb-13 1:41am
v2
Comments
digimanus at 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
Your Filters
Interested
Ignored
     
0 Shai Vashdi 600
1 Tadit Dash 295
2 Sergey Alexandrovich Kryukov 250
3 Peter Leow 190
4 OriginalGriff 158
0 Sergey Alexandrovich Kryukov 9,395
1 OriginalGriff 5,473
2 Peter Leow 4,150
3 Maciej Los 3,540
4 Abhinav S 3,333


Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 25 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid