Click here to Skip to main content
13,259,138 members (47,702 online)
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
Why this issue happened. If It Reads One Parameter then It should Read all

Please Please Tell me why is this Happing ?

<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")
                Label3.Text = xmlRdr.ReadString();
            if (xmlRdr.Name == "State")
            Label7.Text = xmlRdr.ReadString();
            if (xmlRdr.Name == "Postcode")
              Label8.Text = xmlRdr.ReadString();
Posted 24-Feb-13 1:45am
Updated 24-Feb-13 1:56am

1 solution

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

Solution 1

When I run, It Reads Only "Company Name" not All Parameters
This is because 'CompanyName' is at root level and rest others are not - What you have written just reads the root and moves on. You have to move on to descendants.

Here have a look at this article to understand how to read the full XML: MSDN: XmlReader.ReadToDescendant Method (String)[^]

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());

XML it reads:
<!--sample XML fragment-->
  <book genre='novel' ISBN='10-861003-324'>
    <title>The Handmaid's Tale</title>
  <book genre='novel' ISBN='1-861001-57-5'>
    <title>Pride And Prejudice</title>

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 |
Web02 | 2.8.171114.1 | Last Updated 24 Feb 2013
Copyright © CodeProject, 1999-2017
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