Click here to Skip to main content
15,901,283 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
my xml file:
HTML
<PersonelInfo>
  <Person>
    <KeyID>15</KeyID>
    <PersonelCode>12</PersonelCode>
    <Company>sth</Company>
    <FirstName>m</FirstName>
    <LastName>it</LastName>
  </Person>
</PersonelInfo>


reding xml file
C#
using (StringReader stringReader = new StringReader("C:\\Users\\sth\\Desktop\\admin715.xml"))
using (XmlTextReader reader = new XmlTextReader(stringReader))
{
    // Parse the file and display each of the nodes.
    Class_PersonelInfo prsn = null;
    if (reader.IsStartElement())
    {
        while (reader.Read())
        {
            switch (reader.Name)
            {
                case "PersonelCode":
                    prsn.prsnlCod = reader.Value;
                    break;
                case "Company":
                    prsn.jobLocation = reader.Value;
                    break;
                case "FirstName":
                    prsn.frstName = reader.Value;
                    break;
                case "LastName":
                    prsn.lstName = reader.Value;
                    break;
             }
        }
   }
}


the problem is reader is "none"
Posted

Its silly Mistake your StringReaderClass Expects String so Solution would be like this you had to replace your StringReader line like this

C#
StringReader stringReader = new StringReader(File.ReadAllText("C:\\Users\\sth\\Desktop\\admin715.xml"));



You had to pass your string content to StringReaderClass so read all content from your file and then pass it to xmlTextReader Class it will give you proper result.
 
Share this answer
 
v3
Comments
ashok rathod 7-Oct-14 1:08am    
Can anyone tell me what is the reason to downvote the answer although it contains all the required information and is a fullproof solution for the question.
once try like this, this is simple way to read xmal values and assign it to properties

C#
class readxml
   {
       static void Main(string[] args)
       {
           var doc=XElement.Load(@"F:\WebApplication1\ConsoleApplication1\XMLFile1.xml");
           var ms = doc.Elements("Person");
           var test = ms.Select(s => new PresonTest() { FirstName = s.Element("FirstName").Value, LastName = s.Element("LastName").Value, PersonalCode = s.Element("PersonelCode").Value, Company = s.Element("Company").Value });

       }
   }

   public class PresonTest
   {
       public string FirstName { get; set; }
       public string  LastName { get; set; }
       public string PersonalCode { get; set; }
       public string Company { get; set; }
   }
 
Share this answer
 
Comments
Member 11133908 24-Oct-14 2:27am    
thanks a lot.but if one of the elements be empty,ms show that element like this "", and when I want select elements of ms(ms.select..)for empty elements () I have an error that"object reference not set to an instance of an object"
This is the correct pattern used to create such a reader to read a file:
C#
string fileName = //...
using (XmlReader reader = XmlReader.Create(fileName)) {
   //...
}

The class XmlReader is abstract. The concrete implementation will be chosen by the choice of one of those Create factory methods and their parameters. If you are curious what is the runtime type of your reader, use reader.GetType().

By the way, your "manual" and hard-coded way of dealing with XML is bad. It's much better to use serialization in the form of Data Contract: http://msdn.microsoft.com/en-us/library/ms733127%28v=vs.110%29.aspx[^].

To use Data Contracts, you don't have to use WCF itself.

—SA
 
Share this answer
 
v2

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