Click here to Skip to main content
15,901,373 members
Please Sign up or sign in to vote.
4.00/5 (5 votes)
See more:
I have an XML file I need to read and grab some data from.
This data is eventually loaded to an SQL database and the stored procedures that need to load this data can still be used. The current way this data is read needs to be updated and thus my problem.

I'm trying to write a C# program to read this data.

Some of the "segments", "records" whatever you want to call them can exist and even multiple of them or may not exist at all.
This problem of not being there when I'm trying to capture the data is what is currently causing a problem.

The error is something like not accounting for a null condition or something similar.
Posted
Updated 15-May-11 21:50pm
v2
Comments
Fabio V Silva 15-May-11 18:23pm    
Can you show any code? That would help.
Dalek Dave 16-May-11 3:50am    
Edited for Grammar and Readability.

In addition to Wonde solution take a look there-[XML Read][^] for learn various way to Read XML files.
 
Share this answer
 
v2
using System;
using System.Xml;
namespace ReadXml1
{
    class Class1
    {
        static void Main(string[] args)
        {
            // Create an isntance of XmlTextReader and call Read method to read the file
            XmlTextReader textReader = new XmlTextReader("C:\\books.xml");
            textReader.Read();
            // If the node has value
            while (textReader.Read())
            {
                // Move to fist element
                textReader.MoveToElement();
                Console.WriteLine("XmlTextReader Properties Test");
                Console.WriteLine("===================");
                // Read this element's properties and display them on console
                Console.WriteLine("Name:" + textReader.Name);
                Console.WriteLine("Base URI:" + textReader.BaseURI);
                Console.WriteLine("Local Name:" + textReader.LocalName);
                Console.WriteLine("Attribute Count:" + textReader.AttributeCount.ToString());
                Console.WriteLine("Depth:" + textReader.Depth.ToString());
                Console.WriteLine("Line Number:" + textReader.LineNumber.ToString());
                Console.WriteLine("Node Type:" + textReader.NodeType.ToString());
                Console.WriteLine("Attribute Count:" + textReader.Value.ToString());
            }
        }
    }
}
 
Share this answer
 
Comments
Dalek Dave 16-May-11 3:51am    
Good.
You can read xml file using XmlTextReader[^].NET class. However, if you want more control over it, just take a look at Load and save objects to XML using serialization[^]

I hope this will help you well.
 
Share this answer
 
Comments
RaviRanjanKr 15-May-11 22:55pm    
Nice Answer, My 5 :)
Wonde Tadesse 21-May-11 23:06pm    
Thanks RaviRanjankr.
Dalek Dave 16-May-11 3:50am    
Good Answer.
Wonde Tadesse 21-May-11 23:06pm    
Thanks Dalek.
Give it at try with LINQ to XML[^].
It is a great tool under .NET 4.

C#
XElement root = XElement.Load("PurchaseOrder.xml");

// Find all elements of "Address" and select only elements where attribute Type is "Billing"
IEnumerable<xelement> address = from el in root.Elements("Address")
                      where (string)el.Attribute("Type") == "Billing"
                      select el;

foreach (XElement elm in address)
{
    Console.WriteLine(el);
}
</xelement>
 
Share this answer
 
Comments
yesotaso 16-May-11 6:05am    
Is there anything this bloody LINQ doesnt apply? :P
tbh I dont like VB but http://msdn.microsoft.com/en-us/vbasic/bb887653.aspx shows some promising stuff.
Kim Togo 16-May-11 6:17am    
XML literals in VB.NET is cool. Could be great to have this feature in C# :-)
create one object of DataSet class like..
DataSet _ds = new DataSet("MyDataSet");
_ds.ReadXml(@"c:\xml\file\path\abc.xml");

now change the record as per your need....
and update that data in database.......
 
Share this answer
 
Comments
Monika5 6-Mar-14 2:12am    
i have written a query in xml file i.e.

<<Submenu querystring ="select * from Student1(table name)" Dbname="New Student Record"/>





and after that i want the result in C# coding that is the program read the xml file second pick the attributes valuethna make the connection with Datebase and then execute the query plz help me for this
[no name] 6-Mar-14 3:19am    
User Xpath or for/each loop to get the element by name and acess attributes property with attribute name. later use these values

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