Connecting Tech Pros Worldwide Forums | Help | Site Map

c# simple read and return part of an xml file

chris.millar@voyage.co.uk
Guest
 
Posts: n/a
#1: Nov 12 '05
The below xml is saved as a file, i want the quickest way in c# of reading just the DataSource section and returning it as a string, can anyone help me out.

Cheers

Chris.

<NewDataSet>
<DataSource>
<name>Oracle</name>
<IsMaster>0</IsMaster>
<datasourceid>a3a1a14e-51d9-44b4-b0da-c5f564c405c1</datasourceid>
<ConnectionString>User Id=scott;Password="tiger";Data Source='Oracle"</ConnectionString>
<FileName>OracleDAO.dll</FileName>
</DataSource>
<DataSource>
<name>Sql</name>
<IsMaster>1</IsMaster>
<datasourceid>a3a1a14e-51d9-44b4-b0da-b5b564b405b1</datasourceid>
<ConnectionString>User Id=sa;Password="password";Data Source='sqlserver";InitialCatalog="AdeptTest"</ConnectionString>
<FileName>SQLServerDAO.dll</FileName>
</DataSource>
<Provider_Types>
<TYPE_NAME>smallint</TYPE_NAME>
<DATA_TYPE>2</DATA_TYPE>
<DOTNET_TYPE>System.Int16</DOTNET_TYPE>
<COLUMN_SIZE>5</COLUMN_SIZE>
<IS_NULLABLE>true</IS_NULLABLE>
<CASE_SENSITIVE>false</CASE_SENSITIVE>
<SEARCHABLE>3</SEARCHABLE>
<UNSIGNED_ATTRIBUTE>false</UNSIGNED_ATTRIBUTE>
<FIXED_PREC_SCALE>true</FIXED_PREC_SCALE>
<AUTO_UNIQUE_VALUE>true</AUTO_UNIQUE_VALUE>
<LOCAL_TYPE_NAME>smallint</LOCAL_TYPE_NAME>
<IS_LONG>false</IS_LONG>
<BEST_MATCH>true</BEST_MATCH>
<IS_FIXEDLENGTH>true</IS_FIXEDLENGTH>
<datasourceid>a3a1a14e-51d9-44b4-b0da-b5b564b405b1</datasourceid>
<Provider_Types_Id>9f0c843f-964f-4050-b6e0-b0d5a9d7cdbc</Provider_Types_Id>
</Provider_Types>
<NewDataSet>

************************************************** ********************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...

Oleg Tkachenko
Guest
 
Posts: n/a
#2: Nov 12 '05

re: c# simple read and return part of an xml file


chris.millar@voyage.co.uk wrote:
[color=blue]
> The below xml is saved as a file, i want the quickest way in c# of reading just the DataSource section and returning it as a string, can anyone help me out.[/color]

XmlTextReader r = new XmlTextReader("foo.xml");
while (r.Read())
{
if (r.NodeType == XmlNodeType.Element &&
r.Name == "DataSource")
Console.WriteLine(r.ReadOuterXml());
}
r.Close();

--
Oleg Tkachenko
XmlInsider
http://blog.tkachenko.com
Fabio Vazquez
Guest
 
Posts: n/a
#3: Nov 12 '05

re: c# simple read and return part of an xml file


Hi Oleg,

Do you think this methods is faster than direct XPath query?

Thanks!
-----
Fabio Vazquez


"Oleg Tkachenko" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:OGjTMKD4DHA.1868@TK2MSFTNGP10.phx.gbl...[color=blue]
> chris.millar@voyage.co.uk wrote:
>[color=green]
> > The below xml is saved as a file, i want the quickest way in c# of[/color][/color]
reading just the DataSource section and returning it as a string, can anyone
help me out.[color=blue]
>
> XmlTextReader r = new XmlTextReader("foo.xml");
> while (r.Read())
> {
> if (r.NodeType == XmlNodeType.Element &&
> r.Name == "DataSource")
> Console.WriteLine(r.ReadOuterXml());
> }
> r.Close();
>
> --
> Oleg Tkachenko
> XmlInsider
> http://blog.tkachenko.com[/color]


Christoph Schittko [MVP]
Guest
 
Posts: n/a
#4: Nov 12 '05

re: c# simple read and return part of an xml file


This method is definitely faster than an XPath query as you're not even
reading in the enitre document. Executing an XPath query requires reading in
the entire XmlDocument, parsing the XPath expression and then searching the
content of the Xml Document. With Oleg's method you're neither reading the
whole document nor do you need to parse XPath and then search the document.

--
HTH
Christoph Schittko [MVP, XmlInsider]
Software Architect, .NET Mentor

"Fabio Vazquez" <fmvazque@REMOVE_CAPSterra.com.br> wrote in message
news:u66ATaF4DHA.1556@TK2MSFTNGP11.phx.gbl...[color=blue]
> Hi Oleg,
>
> Do you think this methods is faster than direct XPath query?
>
> Thanks!
> -----
> Fabio Vazquez
>
>
> "Oleg Tkachenko" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
> news:OGjTMKD4DHA.1868@TK2MSFTNGP10.phx.gbl...[color=green]
> > chris.millar@voyage.co.uk wrote:
> >[color=darkred]
> > > The below xml is saved as a file, i want the quickest way in c# of[/color][/color]
> reading just the DataSource section and returning it as a string, can[/color]
anyone[color=blue]
> help me out.[color=green]
> >
> > XmlTextReader r = new XmlTextReader("foo.xml");
> > while (r.Read())
> > {
> > if (r.NodeType == XmlNodeType.Element &&
> > r.Name == "DataSource")
> > Console.WriteLine(r.ReadOuterXml());
> > }
> > r.Close();
> >
> > --
> > Oleg Tkachenko
> > XmlInsider
> > http://blog.tkachenko.com[/color]
>
>[/color]


Ryan Trudelle-Schwarz
Guest
 
Posts: n/a
#5: Nov 12 '05

re: c# simple read and return part of an xml file


"chrisybhoy" <chris.millar@voyage.co.uk> wrote
[color=blue]
> The below xml is saved as a file, i want the quickest way in c# of reading[/color]
just the DataSource section and returning it as a string, can anyone help me
out.

Something along these lines should work for ya. This is assume that you only
want to do this once per file. If you are doing it many times, I'd look at
some sort of caching mechanism or perhaps an XPathDocument to avoid
reopening and reading the file.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
XmlTextReader reader = new XmlTextReader(...);

while(reader.Name != "DataSource")
reader.Read();

String result = reader.ReadOuterXml();

reader.Close();
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Oleg Tkachenko
Guest
 
Posts: n/a
#6: Nov 12 '05

re: c# simple read and return part of an xml file


Fabio Vazquez wrote:
[color=blue]
> Do you think this methods is faster than direct XPath query?[/color]

Sure. See yourself. To be able to query XML using XPath you have to load
the whole XML document into memory (into XPathDocumnet or XmlDocument
or XPathDataDocument). XmlReader doesn't need that. It is the lowest
level of XML processing in .NET hence the fastest. It's quite another
matter that XmlReader may not always be the best way. XmlReader is
forward-only non-caching facility, so for sure doesn't fit all needs,
say it's bad idea to use XmlReader to perform repetitive sophisticated
queries.
--
Oleg Tkachenko
XmlInsider
http://blog.tkachenko.com
Fabio Vazquez
Guest
 
Posts: n/a
#7: Nov 12 '05

re: c# simple read and return part of an xml file


Perfect!

Thank you very much Oleg!
-----
Fabio Vazquez


"Oleg Tkachenko" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:OLmUW5N4DHA.360@TK2MSFTNGP12.phx.gbl...[color=blue]
> Fabio Vazquez wrote:
>[color=green]
> > Do you think this methods is faster than direct XPath query?[/color]
>
> Sure. See yourself. To be able to query XML using XPath you have to load
> the whole XML document into memory (into XPathDocumnet or XmlDocument
> or XPathDataDocument). XmlReader doesn't need that. It is the lowest
> level of XML processing in .NET hence the fastest. It's quite another
> matter that XmlReader may not always be the best way. XmlReader is
> forward-only non-caching facility, so for sure doesn't fit all needs,
> say it's bad idea to use XmlReader to perform repetitive sophisticated
> queries.
> --
> Oleg Tkachenko
> XmlInsider
> http://blog.tkachenko.com[/color]


Fabio Vazquez
Guest
 
Posts: n/a
#8: Nov 12 '05

re: c# simple read and return part of an xml file


Thanks Christoph!

This is perfectly clear!
-----
Fabio Vazquez


"Christoph Schittko [MVP]" <christophdotnetINVALID@austin.rr.com> wrote in
message news:#m5gukK4DHA.2572@TK2MSFTNGP09.phx.gbl...[color=blue]
> This method is definitely faster than an XPath query as you're not even
> reading in the enitre document. Executing an XPath query requires reading[/color]
in[color=blue]
> the entire XmlDocument, parsing the XPath expression and then searching[/color]
the[color=blue]
> content of the Xml Document. With Oleg's method you're neither reading the
> whole document nor do you need to parse XPath and then search the[/color]
document.[color=blue]
>
> --
> HTH
> Christoph Schittko [MVP, XmlInsider]
> Software Architect, .NET Mentor
>
> "Fabio Vazquez" <fmvazque@REMOVE_CAPSterra.com.br> wrote in message
> news:u66ATaF4DHA.1556@TK2MSFTNGP11.phx.gbl...[color=green]
> > Hi Oleg,
> >
> > Do you think this methods is faster than direct XPath query?
> >
> > Thanks!
> > -----
> > Fabio Vazquez
> >
> >
> > "Oleg Tkachenko" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
> > news:OGjTMKD4DHA.1868@TK2MSFTNGP10.phx.gbl...[color=darkred]
> > > chris.millar@voyage.co.uk wrote:
> > >
> > > > The below xml is saved as a file, i want the quickest way in c# of[/color]
> > reading just the DataSource section and returning it as a string, can[/color]
> anyone[color=green]
> > help me out.[color=darkred]
> > >
> > > XmlTextReader r = new XmlTextReader("foo.xml");
> > > while (r.Read())
> > > {
> > > if (r.NodeType == XmlNodeType.Element &&
> > > r.Name == "DataSource")
> > > Console.WriteLine(r.ReadOuterXml());
> > > }
> > > r.Close();
> > >
> > > --
> > > Oleg Tkachenko
> > > XmlInsider
> > > http://blog.tkachenko.com[/color]
> >
> >[/color]
>
>[/color]


Closed Thread