Click here to Skip to main content
15,919,479 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i Have Created A xml File like Bellow

XML
<?xml version="1.0" encoding="utf-8"?>
<PurchaseBill>
  <PartyId>1</PartyId>
  <BillNo1>1</BillNo>
  <BillAmount>2450</BillAmount>
  <DiscountAmount>1000</DiscountAmount>
  <Products>
    <ProductId>1</ProductId>
    <Quantity>10</Quantity>
    <Mrp>100</Mrp>
  </Products>
  <Products>
    <ProductId>2</ProductId>
    <Quantity>10</Quantity>
    <Mrp>50</Mrp>
  </Products>
  <Products>
    <ProductId>3</ProductId>
    <Quantity>10</Quantity>
    <Mrp>75</Mrp>
  </Products>
 <Products>
    <ProductId>4</ProductId>
    <Quantity>1</Quantity>
    <Mrp>100</Mrp>
  </Products>
 <Products>
    <ProductId>5</ProductId>
    <Quantity>10</Quantity>
    <Mrp>10</Mrp>
  </Products>
</PurchaseBill>



Now i am trying to read this file....
Pls Help

Thanks In Advance
Posted

Here's Great MSDN article regarding Reading XML[^] or you will find tons of article if you give a try to your search friend Google before.
 
Share this answer
 
What's better than MSDN documentation: "How to read XML from a file by using Visual C#"[^]?

:)
 
Share this answer
 
Hi Yatin..
May this Help u

using System;
using System.Xml;
namespace ReadingXML2
{
class Class1
{
static void Main(string[] args)
{
int ws = 0;
int pi = 0;
int dc = 0;
int cc = 0;
int ac = 0;
int et = 0;
int el = 0;
int xd = 0;
// Read a document
XmlTextReader textReader = new XmlTextReader("C:\\books.xml");
// Read until end of file
while (textReader.Read())
{
XmlNodeType nType = textReader.NodeType;
// If node type us a declaration
if (nType == XmlNodeType.XmlDeclaration)
{
Console.WriteLine("Declaration:" + textReader.Name.ToString());
xd = xd + 1;
}
// if node type is a comment
if (nType == XmlNodeType.Comment)
{
Console.WriteLine("Comment:" + textReader.Name.ToString());
cc = cc + 1;
}
// if node type us an attribute
if (nType == XmlNodeType.Attribute)
{
Console.WriteLine("Attribute:" + textReader.Name.ToString());
ac = ac + 1;
}
// if node type is an element
if (nType == XmlNodeType.Element)
{
Console.WriteLine("Element:" + textReader.Name.ToString());
el = el + 1;
}
// if node type is an entity\
if (nType == XmlNodeType.Entity)
{
Console.WriteLine("Entity:" + textReader.Name.ToString());
et = et + 1;
}
// if node type is a Process Instruction
if (nType == XmlNodeType.Entity)
{
Console.WriteLine("Entity:" + textReader.Name.ToString());
pi = pi + 1;
}
// if node type a document
if (nType == XmlNodeType.DocumentType)
{
Console.WriteLine("Document:" + textReader.Name.ToString());
dc = dc + 1;
}
// if node type is white space
if (nType == XmlNodeType.Whitespace)
{
Console.WriteLine("WhiteSpace:" + textReader.Name.ToString());
ws = ws + 1;
}
}
// Write the summary
Console.WriteLine("Total Comments:" + cc.ToString());
Console.WriteLine("Total Attributes:" + ac.ToString());
Console.WriteLine("Total Elements:" + el.ToString());
Console.WriteLine("Total Entity:" + et.ToString());
Console.WriteLine("Total Process Instructions:" + pi.ToString());
Console.WriteLine("Total Declaration:" + xd.ToString());
Console.WriteLine("Total DocumentType:" + dc.ToString());
Console.WriteLine("Total WhiteSpaces:" + ws.ToString());
}
}
}



if any probs .. check this link
www.c-sharpcorner.com/uploadfile/mahesh/readwritexmltutmellli2111282005041517am/readwritexmltutmellli21.aspx



regards,
Saran.t
 
Share this answer
 
Hi Yatin,

to get yourself started, go here and read the method
btnLoad_Click. This will at least show you how to read
the xml file into an XmlDocument:

http://www.codeproject.com/KB/XML/csreadxml1.aspx

(Hint: Always use the search function of CodeProject first, before asking a question)

Cheers

Manfred
 
Share this answer
 
Use LINQ with XML

C#
XDocument db = XDocument.Load("File.xml");
            var purchase = from p in db.Descendants("PurchaseBill")
                    select p;
            foreach (var item in purchase)
            {
                Console.WriteLine("PartyId: " +item.Element("PartyId").Value);
                Console.WriteLine("BillNo: " + item.Element("BillNo").Value);
                Console.WriteLine("BillAmount: " + item.Element("BillAmount").Value);
                Console.WriteLine("DiscountAmount: " + item.Element("DiscountAmount").Value);

            }
            Console.WriteLine("Products ....");
            var products = from p in db.Descendants("Products")
                           select p;

            foreach (var item in products)
            {
                Console.WriteLine("ProductId: " + item.Element("ProductId").Value);
                Console.WriteLine("Quantity: " + item.Element("Quantity").Value);
                Console.WriteLine("Mrp: " + item.Element("Mrp").Value);
            }
            Console.ReadLine();
 
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