Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: XML
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 ?
 
<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 24-Feb-13 1:45am
Edited 24-Feb-13 1:56am
v4

1 solution

Rate this: bad
good
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)[^]
 

Example:
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:
<!--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>
  Permalink  

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

  Print Answers RSS
0 George Jonsson 175
1 Kornfeld Eliyahu Peter 169
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 24 Feb 2013
Copyright © CodeProject, 1999-2014
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