Click here to Skip to main content
15,886,611 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
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 ?

XML
<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>
                    <Postcode>3083</Postcode>
                </Address>
            </CompanyDetails>
            <ContactDetails>

                <ContactName>JACQUELINE MAO</ContactName>
                <PhoneNumber>0394585893</PhoneNumber>
                <MobileNumber>0418885185</MobileNumber>


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
Updated 24-Feb-13 0:56am
v4

1 solution

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)[^]


Example:
C#
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());

}

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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900