-1

I am totally new to reading data from file, specially xml files. I have a xml file in my bin\debug folder and the xml file looks something like this:

<Companies>
    <Company>
        <Name>Name1</Name><Code>11014</Code>    
        <MaintenancePercentage>15.9</MaintenancePercentage>
        <Sales>
            <Sale>
                <Code>19538</Code>
                <Title>ABC</Title>
                <Date>2009-04-29T00:00:00</Date>
                <Category>Category1</Category>
                <Amount>6543.39</Amount>
            </Sale>
            <Sale>
                <Code>19539</Code>
                <Title>xyz</Title>
                <Date>2009-04-30T00:00:00</Date>
                <Category>Category2</Category>
                <Amount>654.39</Amount>
            </Sale>
        </Sales>
    </Company>

    <Company>
        <Name>Name1</Name><Code>11014</Code>     
        <MaintenancePercentage>15.9</MaintenancePercentage>
        <Sales>
            <Sale>
                <Code>19538</Code>
                <Title>ABC</Title>
                <Date>2009-04-29T00:00:00</Date>
                <Category>Category1</Category>
                <Amount>6543.39</Amount>
            </Sale>
            <Sale>
                <Code>19539</Code>
                <Title>xyz</Title>
                <Date>2009-04-30T00:00:00</Date>
                <Category>Category2</Category>
                <Amount>654.39</Amount>
            </Sale>
        </Sales>
    </Company>
</Companies>

This are my classes:

public class Company
{
    public string Name;
    public string Code;
    public double MaintenancePercentage;
    public double AverageSales;
    public double TotalSales;
    public double TotalMaintenanceFee;
    public List<Sales> Saleses;

}
public class Sales
{
    public string Code;
    public string Title;
    public DateTime DateTime;
    public string Category;
    public double Amount;
}

Now from the xml file I want to read the data and store them.

How can I read the values from the xml files?

For example

var company = new Company();
company.Name = //name from xml file
//and so on
foreach(sales in company)
{
   sales.code = //code from xml file
   sales.title = //title from xml file
   //and so on
}

This is what I've done so far using search results from google, but I don't know what to do now

var doc = new XmlDocument();
doc.Load(@"export.xml");
var root = doc.DocumentElement;
if (root == null)
{
    return;
}
var company = root.SelectNodes("Company");
if (company == null)
{
    return;
}
else
{
    foreach (var companyData in company)
    {
        var title = 
    }
}

This last piece of code makes any sense at least?

Balagurunathan Marimuthu
  • 2,927
  • 4
  • 31
  • 44
Cybercop
  • 8,475
  • 21
  • 75
  • 135
  • 2
    What have you tried? I just googled "c# how to read xml files" and came up with a whole heap of good tutorials. – Immortal Blue Oct 22 '13 at 08:22
  • Starting [here](http://msdn.microsoft.com/en-us/library/system.xml.linq%28v=vs.110%29.aspx) couldn't hurt. – ChiefTwoPencils Oct 22 '13 at 08:22
  • I would suggest you to read about the [XmlSerializer](http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer(v=vs.80).aspx)(good example included) and about [public fields vs properties](http://stackoverflow.com/questions/653536/difference-between-property-and-field-in-c-sharp-3-0). –  Oct 22 '13 at 08:25

2 Answers2

1

Define a Companies class, which just contains a list of Company, Then you can use code along the following lines to deserialize the whole lot to your set of objects:

var serializer = new XmlSerializer(typeof(Companies));
var reader = new StreamReader(pathToXmlFile);
var companies = (Companies)serializer.Deserialize(reader);
reader.Close();
David Arno
  • 42,717
  • 16
  • 86
  • 131
0

Suppose Your Test.XML file in debug folder

        var doc = (from e in XDocument.Load("Test.xml").Root.Elements("Company")
                           select new Company
                              {    
                                 name=(string)e.Element("name"),
                                 MaintenancePercentage=(double)e.Element("MaintenancePercentage"),
                                 Sales=(from sl in e.Elements("Sales").Elements("Sale")
                                     select new Sals
                                       {
                                          Code=(string)sl.Element("Code"),
                                          Title=(string)sl.Element("Title"),
                                          Datetime=(DateTime)sl.Elemnt("Date"),
                                          Category==(string)sl.Element("Category"),
                                          Amount=(double)sl.Element("Amount")

                                       }).ToArray()
                              }).ToList();
var company = new Company();
foreach(var Comp in doc)
{
    company.name=Comp.name;
    company.MaintenancePercentage=Comp.MaintenancePercentage;

}
var cSales=new Sales();
foreach(var salas in doc.Sales)
{

        cSales.Code=salas.Code;
        cSales.Title=salas.Title;
        pcSales.DateTime=salas.Datetime;
        cSales.Category=salas.Category;
       cSales.Amount=salas.Amount;
}

I hop you get your solution ...

Vijay Mungara
  • 83
  • 1
  • 5