473,403 Members | 2,183 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Parsing XML from a String

I'm new to C# from a C++ background.

A problem I'm having is getting my head around the different XML classes
available.

In particular I have a String type containing XML and want to parse it to
extract values. The XML classes I've seen so far will do it from a file or a
stream but (unless my eyes have failed) I can't find one to do it directly
from a String.

Any help appreciated!

Lindsay
Nov 16 '05 #1
8 62339
Use the Load() method from XMLDocument object.
LoadXml() method is used for loading xml from .xml file.

Regards,
Amal

"Lindsay" wrote:
I'm new to C# from a C++ background.

A problem I'm having is getting my head around the different XML classes
available.

In particular I have a String type containing XML and want to parse it to
extract values. The XML classes I've seen so far will do it from a file or a
stream but (unless my eyes have failed) I can't find one to do it directly
from a String.

Any help appreciated!

Lindsay

Nov 16 '05 #2
Hello Lindsay,

If you already have the XML in a string format, then you can use the
InnerText property of the XmlDocument to create a DOM Tree. In the example
below, I have a string which contains the xml. I contruct a DOM Tree and
parse its attributes.

using System;
using System.IO;
using System.Xml;

public class XmlFromString
{
public static void Main(string[] args)
{
string xml = "<?xml version='1.0'?><person firstname='john'
lastname='smith' />";
XmlDocument doc = new XmlDocument();
doc.InnerXml = xml;
XmlElement root = doc.DocumentElement;
Console.WriteLine(" The firstname : {0} lastname: {1}",
root.GetAttribute("firstname"), root.GetAttribute("lastname"));
}
}

regards,
Abhishek.
"Lindsay" <do_not_email_me@i_hate_spam.com> wrote in message
news:#6**************@TK2MSFTNGP10.phx.gbl...
I'm new to C# from a C++ background.

A problem I'm having is getting my head around the different XML classes
available.

In particular I have a String type containing XML and want to parse it to
extract values. The XML classes I've seen so far will do it from a file or a stream but (unless my eyes have failed) I can't find one to do it directly
from a String.

Any help appreciated!

Lindsay

Nov 16 '05 #3
Thanks Guys!

"Lindsay" <do_not_email_me@i_hate_spam.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
I'm new to C# from a C++ background.

A problem I'm having is getting my head around the different XML classes
available.

In particular I have a String type containing XML and want to parse it to
extract values. The XML classes I've seen so far will do it from a file or a stream but (unless my eyes have failed) I can't find one to do it directly
from a String.

Any help appreciated!

Lindsay

Nov 16 '05 #4
I would also suggest the XmlTextReader as another option. This provides fast, forward only reading of xml. To parse an Xml string, use the following:

XmlTextReader reader = new XmlTextReader(new System.IO.StringReader(xmlString));

Cheers
Dan
"Lindsay" wrote:
I'm new to C# from a C++ background.

A problem I'm having is getting my head around the different XML classes
available.

In particular I have a String type containing XML and want to parse it to
extract values. The XML classes I've seen so far will do it from a file or a
stream but (unless my eyes have failed) I can't find one to do it directly
from a String.

Any help appreciated!

Lindsay

Nov 16 '05 #5
string result;
DataSet ds = new DataSet();
TextWriter tw = new StreamWriter("XMLString");
tw.WriteLine(result.ToString()); //result is your string input
tw.Close();
ds.ReadXml("XMLString");

foreach(DataRow dr in ds.Tables[0].Rows)
{
Console.WriteLine(dr["put the element here"]); ......
}

Hope this helps!
Nov 16 '05 #6
GeRmIc wrote:
string result;
DataSet ds = new DataSet();
TextWriter tw = new StreamWriter("XMLString");
tw.WriteLine(result.ToString()); //result is your string input
tw.Close();
ds.ReadXml("XMLString");

foreach(DataRow dr in ds.Tables[0].Rows)
{
Console.WriteLine(dr["put the element here"]); ......
}

Hope this helps!


Don't get me wrong, but this is incredibly awkward. There are numerous ways
to parse XML from a string without performing rain dances like that one.

Cheers,
--
Joerg Jooss
jo*********@gmx.net
Nov 16 '05 #7
Hi Jooss,

I know, but does'nt this simple code gets the job done? You could perhaps
post some other ways to do it too so that the next person could benefit from
it.

Cheers!

"Joerg Jooss" wrote:
GeRmIc wrote:
string result;
DataSet ds = new DataSet();
TextWriter tw = new StreamWriter("XMLString");
tw.WriteLine(result.ToString()); //result is your string input
tw.Close();
ds.ReadXml("XMLString");

foreach(DataRow dr in ds.Tables[0].Rows)
{
Console.WriteLine(dr["put the element here"]); ......
}

Hope this helps!


Don't get me wrong, but this is incredibly awkward. There are numerous ways
to parse XML from a string without performing rain dances like that one.

Cheers,
--
Joerg Jooss
jo*********@gmx.net

Nov 16 '05 #8
GeRmIc wrote:
Hi Jooss,

I know, but does'nt this simple code gets the job done?
No, it's absolutely not simple. It writes the string back to disk, and then
uses an in-memory database for parsing the file.

No, it won't get the job done as expected. Put this code in a library and
try to reuse it from an ASP.NET application -- no joy, because the worker
process cannot write to its own virtual directory without changing
permissions.
You could
perhaps post some other ways to do it too so that the next person
could benefit from it.


You should have read the entire thread. XmlDocument.LoadXml() is all you
need.

Cheers,
--
Joerg Jooss
jo*********@gmx.net
Nov 16 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: meldrape | last post by:
Hello, I need to parse a long string into no more than 30 character chunks, but I also need to leave the words intact. Right now, I am using: For intStart = 1 to Len(strOriginal) by 30...
7
by: Daniel Lidström | last post by:
Hi, I'm currently using this method to extract doubles from a string: System::String* sp = S" "; System::String* tokens = s->Trim()->Split(sp->ToCharArray()); m_Northing =...
15
by: John Smith | last post by:
I would like to parse a string into an array. I found on the net the following codes which parse a string and print it. The result is exactly what I want: char * pch; pch = strtok (buffer," ");...
6
by: Computer_Czar | last post by:
I'm trying to figure out the best way to parse an input string from a file for hex values. The string is actually Motorola S code produced by an embedded assembler. For example lines contain...
3
by: LEM | last post by:
Hi all, I'm new to C# and I'm trying to parse a string into several variables. I used to do this in C with sscanf, but I don't know how to do it in C#. Basically I have this. String cBuffer...
4
by: Michael Meckelein | last post by:
Hello, Wondering, if C# (framework 2.0) does not support parsing DateTime timezones in three letter acronyms. I would like to parse date strings like "2005 Nov 01 11:58:47.490 CST -6:00" but...
3
by: Rich Shepard | last post by:
I need to learn how to process a byte stream from a form reader where each pair of bytes has meaning according to lookup dictionaries, then use the values to build an array of rows inserted into a...
7
by: Grey Alien | last post by:
Does *ANYONE* in here know how I may parse the various date/time 'elements' from a string?. The input string has the ff format: 'YYYY-MM-DD HH:MM:SS AM'
7
by: Donn Ingle | last post by:
Hi, I really hope someone can help me -- I'm stuck. I have written three versions of code over a week and still can't get past this problem, it's blocking my path to getting other code written. ...
5
by: tech | last post by:
Hi, I need to parse a string used to represent a time and then populate a simple time struct. The time string will always be this format 23:45.45 ie hours separated from mins by ':' and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.