Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 5th, 2005, 12:08 AM
Registered User
 
Join Date: Aug 2005
Posts: 9
Thanks: 0
Thanked 1 Time in 1 Post
Default progamming XML in C#

Hi...

Can anyone help me out with progamming XML in C#....
I have been trying to convert an xml file into a stream (string builder) in C#....
I first started out with filestream class....
the stream reader to read the xml stream and then just tried to write it to the stringbuilder....:(

but I was not able to read the XML file using stream reader....
once u convert it to a stream why is not possible to read it....
plz help me out with this....
and reply to this addr: [email protected]

thanku


 
Old August 6th, 2005, 01:02 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

for reading XML files you have two options
1-you want to just read it as a simple text file irregardless of its format
Code:
            using(StreamReader reader=new StreamReader("C://test.xml"))
            {
                textBox1.Text=reader.ReadToEnd();
            }
            2-you want to read it but for parsing purposes
Code:
            System.Xml.XmlTextReader xmlr=new XmlTextReader("C://test.xml");
Code:
            System.Xml.XmlValidatingReader vmv=new XmlValidatingReader(xmlr);
            //check if it has DTD info otherwise it doesn't check it
            vmv.ValidationType=ValidationType.Auto;
            //if it finds a error this event will be raised
            vmv.ValidationEventHandler+=new System.Xml.Schema.ValidationEventHandler(vmv_ValidationEventHandler);
            System.Xml.XmlDocument xmd=new XmlDocument();
            try
            {
                xmd.Load(vmv);
            }
            catch(XmlException){textBox1.Text="your XML file is not well-formed";}
            textBox1.Text=xmd.OuterXml;


_____________
Mehdi.
software student.
 
Old August 7th, 2005, 09:49 PM
Registered User
 
Join Date: Aug 2005
Posts: 9
Thanks: 0
Thanked 1 Time in 1 Post
Default

Thanku Mehdi.....
could u plz clear another small doubt i have...
I had written this code and till i included two statements "wr.flush" and "ms.flush"...programme was not giving any o/p....
wr is an streamwriter instance .... why would we do wr.flush to an instance.....I can understand "ms.flush" where ms is a memorystream instance....

MemoryStream ms = new MemoryStream();
            string fileName = "c:/excercise.xml";
            FileStream fs = new FileStream(fileName,FileMode.Open);
            StreamReader sr = new StreamReader(fs);
            string str = sr.ReadToEnd();
            sr.Close();
            StreamWriter wr = new StreamWriter(ms);
            wr.Write(str);
            wr.Flush();
            ms.Flush();

            ms.Position=0;
            StreamReader ar = new StreamReader(ms);

            string iu = ar.ReadToEnd();
            Console.WriteLine(iu);

 
Old August 8th, 2005, 01:12 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

well wr.Flush updates the uderlying stream which is the MemoryStream
and ms.Flush updates the memory..mainly ms.Flush is something redundant.

_____________
Mehdi.
software student.
 
Old August 9th, 2005, 07:28 AM
Registered User
 
Join Date: Aug 2005
Posts: 9
Thanks: 0
Thanked 1 Time in 1 Post
Default

Thanks Mehdi....
It was of very g8 help.....






Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL Server 2005 XML: FOR XML PATH -> cdata? stoves SQL Server 2005 1 July 8th, 2008 02:40 AM
Creating XML doc ; writing string(xml format) into KamalRaturi XML 5 May 28th, 2008 05:51 AM
VB.net, adding XML data to an existing XML file saikoboarder XML 11 April 17th, 2008 04:19 PM
xml invalid top level from ASP write XML(solution) g000we XML 0 August 9th, 2006 03:56 AM
DTS Package, XML task. Read XML file and store it Victoria SQL Server DTS 0 July 24th, 2006 02:43 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.