473,748 Members | 8,530 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Read XML from string instead of file (C#)

I've searched quite a bit for this one and i can't find any solid
solution for what i'm doing here.

Right now i'm geting an xml string from an API, creating an xml file,
then read the file with XPath or XmlReader, grab the attribute data,
dump it into the database.
Once all that's done i blow away all the xml files and start the whole
process over next time i need to read fresh data from the API.
Question is, how can i just read the returned xml string from the API
and parse out all my data without having to deal with creating,
reading, dumping all these files.
Should be doable i think...
I know this is probably kind of a noob question, but i'm no xml guru,
obviously...
Thanks for any info...

Jun 2 '06 #1
5 9102
On 1 Jun 2006 21:40:43 -0700, "th3dude" <gn*****@gmail. com> wrote:
Question is, how can i just read the returned xml string from the API
and parse out all my data without having to deal with creating,
reading, dumping all these files.


You need to create a MemoryStream out of it. The example I'm pasting
below I created a DOM out of the XML but I think you can morph it into
what you want.

public static List<Membership ProfileInfo>
CheckXMLForProf ileData(string xmlData,string userName,out
string errorString)
{
// take in this xml and look for profile data
// <profile defaultProvider ="SqlProfilePro vider">
// <providers>
// <remove name="AspNetSql ProfileProvider "/>
// <add name="SqlProfil eProvider"
type="System.We b.Profile.SqlPr ofileProvider"
// connectionStrin gName="LocalSql Server"/>
// </providers>
// <properties>
// <add name="advancedM ode" type="bool"/>
// <add name="defaultCo nnectionName"
type="string"/>
// <add name="defaultNa meSpaceGenerate d"
type="string"/>
// <add name="debugMode " type="bool"/>
// <add name="FirstName " type="string"/>
// <add name="LastName" type="string"/>
// <add name="City" type="string"/>
// <add name="State" type="string"/>
// <add name="Country" type="string"/>
// <add name="Company" type="string"/>
// </properties>
//</profile>

errorString = string.Empty;
byte[] byteArray = new byte[xmlData.Length];
System.Text.ASC IIEncoding encoding = new
System.Text.ASC IIEncoding();
byteArray = encoding.GetByt es(xmlData);

// Load the memory stream
MemoryStream memoryStream = new MemoryStream(by teArray);
//XmlDocument doc = new XmlDocument();
memoryStream.Se ek(0, SeekOrigin.Begi n);

List<Membership ProfileInfo> mpiLi = new
List<Membership ProfileInfo>();

if (byteArray.Leng th > 0)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(memory Stream);

string xPath1 = "/system.web/profile/properties";
XmlNodeList nodeList = doc.SelectNodes (xPath1);

foreach (XmlNode nodex in nodeList)
{
if (nodex.HasChild Nodes)
{
foreach (XmlNode nodexx in
nodex.ChildNode s)
{
if (nodexx.Name.Eq uals("group"))
{
// Get Group Name
MembershipProfi leInfo mpi = new
MembershipProfi leInfo();
mpi = GetAttrs(nodexx ,
string.Empty);
string prefixName = mpi.Name;
foreach (XmlNode nodexxx in
nodexx.ChildNod es)
{
MembershipProfi leInfo mpi1 =
new MembershipProfi leInfo();
mpi1 = GetAttrs(nodexx x,
prefixName);
mpiLi.Add(mpi1) ;
}
}
else if (nodexx.Name.Eq uals("add"))
{
MembershipProfi leInfo mpi = new
MembershipProfi leInfo();
mpi = GetAttrs(nodexx ,
string.Empty);
mpiLi.Add(mpi);
}
}
}
}
}
catch (Exception eee)
{
errorString = eee.ToString();
}
}
return mpiLi;
}
Peter Kellner
http://peterkellner.net
Jun 2 '06 #2
Peter,

Thanks for the reply, i'll give this a shot and see if i can get it to
work.

Cheers.

PeterKellner wrote:
On 1 Jun 2006 21:40:43 -0700, "th3dude" <gn*****@gmail. com> wrote:
Question is, how can i just read the returned xml string from the API
and parse out all my data without having to deal with creating,
reading, dumping all these files.


You need to create a MemoryStream out of it. The example I'm pasting
below I created a DOM out of the XML but I think you can morph it into
what you want.

public static List<Membership ProfileInfo>
CheckXMLForProf ileData(string xmlData,string userName,out
string errorString)
{
// take in this xml and look for profile data
// <profile defaultProvider ="SqlProfilePro vider">
// <providers>
// <remove name="AspNetSql ProfileProvider "/>
// <add name="SqlProfil eProvider"
type="System.We b.Profile.SqlPr ofileProvider"
// connectionStrin gName="LocalSql Server"/>
// </providers>
// <properties>
// <add name="advancedM ode" type="bool"/>
// <add name="defaultCo nnectionName"
type="string"/>
// <add name="defaultNa meSpaceGenerate d"
type="string"/>
// <add name="debugMode " type="bool"/>
// <add name="FirstName " type="string"/>
// <add name="LastName" type="string"/>
// <add name="City" type="string"/>
// <add name="State" type="string"/>
// <add name="Country" type="string"/>
// <add name="Company" type="string"/>
// </properties>
//</profile>

errorString = string.Empty;
byte[] byteArray = new byte[xmlData.Length];
System.Text.ASC IIEncoding encoding = new
System.Text.ASC IIEncoding();
byteArray = encoding.GetByt es(xmlData);

// Load the memory stream
MemoryStream memoryStream = new MemoryStream(by teArray);
//XmlDocument doc = new XmlDocument();
memoryStream.Se ek(0, SeekOrigin.Begi n);

List<Membership ProfileInfo> mpiLi = new
List<Membership ProfileInfo>();

if (byteArray.Leng th > 0)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(memory Stream);

string xPath1 = "/system.web/profile/properties";
XmlNodeList nodeList = doc.SelectNodes (xPath1);

foreach (XmlNode nodex in nodeList)
{
if (nodex.HasChild Nodes)
{
foreach (XmlNode nodexx in
nodex.ChildNode s)
{
if (nodexx.Name.Eq uals("group"))
{
// Get Group Name
MembershipProfi leInfo mpi = new
MembershipProfi leInfo();
mpi = GetAttrs(nodexx ,
string.Empty);
string prefixName = mpi.Name;
foreach (XmlNode nodexxx in
nodexx.ChildNod es)
{
MembershipProfi leInfo mpi1 =
new MembershipProfi leInfo();
mpi1 = GetAttrs(nodexx x,
prefixName);
mpiLi.Add(mpi1) ;
}
}
else if (nodexx.Name.Eq uals("add"))
{
MembershipProfi leInfo mpi = new
MembershipProfi leInfo();
mpi = GetAttrs(nodexx ,
string.Empty);
mpiLi.Add(mpi);
}
}
}
}
}
catch (Exception eee)
{
errorString = eee.ToString();
}
}
return mpiLi;
}
Peter Kellner
http://peterkellner.net


Jun 2 '06 #3
Peter,

I ended up doing something very similar to this, but i was able to get
it to work using jsut a string and not creating the memory stream.

Your post did point me in the right direction though so thanks again!
th3dude wrote:
Peter,

Thanks for the reply, i'll give this a shot and see if i can get it to
work.

Cheers.

PeterKellner wrote:
On 1 Jun 2006 21:40:43 -0700, "th3dude" <gn*****@gmail. com> wrote:
Question is, how can i just read the returned xml string from the API
and parse out all my data without having to deal with creating,
reading, dumping all these files.


You need to create a MemoryStream out of it. The example I'm pasting
below I created a DOM out of the XML but I think you can morph it into
what you want.

public static List<Membership ProfileInfo>
CheckXMLForProf ileData(string xmlData,string userName,out
string errorString)
{
// take in this xml and look for profile data
// <profile defaultProvider ="SqlProfilePro vider">
// <providers>
// <remove name="AspNetSql ProfileProvider "/>
// <add name="SqlProfil eProvider"
type="System.We b.Profile.SqlPr ofileProvider"
// connectionStrin gName="LocalSql Server"/>
// </providers>
// <properties>
// <add name="advancedM ode" type="bool"/>
// <add name="defaultCo nnectionName"
type="string"/>
// <add name="defaultNa meSpaceGenerate d"
type="string"/>
// <add name="debugMode " type="bool"/>
// <add name="FirstName " type="string"/>
// <add name="LastName" type="string"/>
// <add name="City" type="string"/>
// <add name="State" type="string"/>
// <add name="Country" type="string"/>
// <add name="Company" type="string"/>
// </properties>
//</profile>

errorString = string.Empty;
byte[] byteArray = new byte[xmlData.Length];
System.Text.ASC IIEncoding encoding = new
System.Text.ASC IIEncoding();
byteArray = encoding.GetByt es(xmlData);

// Load the memory stream
MemoryStream memoryStream = new MemoryStream(by teArray);
//XmlDocument doc = new XmlDocument();
memoryStream.Se ek(0, SeekOrigin.Begi n);

List<Membership ProfileInfo> mpiLi = new
List<Membership ProfileInfo>();

if (byteArray.Leng th > 0)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(memory Stream);

string xPath1 = "/system.web/profile/properties";
XmlNodeList nodeList = doc.SelectNodes (xPath1);

foreach (XmlNode nodex in nodeList)
{
if (nodex.HasChild Nodes)
{
foreach (XmlNode nodexx in
nodex.ChildNode s)
{
if (nodexx.Name.Eq uals("group"))
{
// Get Group Name
MembershipProfi leInfo mpi = new
MembershipProfi leInfo();
mpi = GetAttrs(nodexx ,
string.Empty);
string prefixName = mpi.Name;
foreach (XmlNode nodexxx in
nodexx.ChildNod es)
{
MembershipProfi leInfo mpi1 =
new MembershipProfi leInfo();
mpi1 = GetAttrs(nodexx x,
prefixName);
mpiLi.Add(mpi1) ;
}
}
else if (nodexx.Name.Eq uals("add"))
{
MembershipProfi leInfo mpi = new
MembershipProfi leInfo();
mpi = GetAttrs(nodexx ,
string.Empty);
mpiLi.Add(mpi);
}
}
}
}
}
catch (Exception eee)
{
errorString = eee.ToString();
}
}
return mpiLi;
}
Peter Kellner
http://peterkellner.net


Jun 5 '06 #4
Not sure if I'm missing something, but I just used a StringReader:

string xml = null;

//load the xml into the string, etc. etc.

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(new StringReader(xm l));

Cheers

Remy

Jun 5 '06 #5
Well my original issue was happening because of some odd Xpath
behavior, so when i
changed to XmlNodeList/XmlElement and dumped Xpath everything worked
much better fof this particular application...

And yes the following works well for me now...

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml string);

Thanks,
--gary

Remy wrote:
Not sure if I'm missing something, but I just used a StringReader:

string xml = null;

//load the xml into the string, etc. etc.

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(new StringReader(xm l));

Cheers

Remy


Jun 5 '06 #6

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

Similar topics

2
6710
by: kak3012 | last post by:
Hi, I have a text file I will read it and write out binary. The file includes 256 coloums. I use while (infile.good()) { infile.getline (buffer,2200);
4
1862
by: pisscot | last post by:
Lind Apr 13, 6:32 am show options Newsgroups: comp.lang.c From: piss...@gmail.com (Lind) - Find messages by this author Date: 13 Apr 2005 06:32:15 -0700 Local: Wed,Apr 13 2005 6:32 am Subject: how to read it out using c++ Reply | Reply to Author | Forward | Print | Individual Message | Show original | Remove | Report Abuse
4
2494
by: yo_mismo | last post by:
Hi everyone, I'm trying to read the first line of a file this way: .... .... .... .... new_line=0; while((read=read(fd, &info, sizeof(info))) > 0 && !new_line){ if (strcmp(&info, "\n") != 0){
5
2926
by: Pete | last post by:
I having a problem reading all characters from a file. What I'm trying to do is open a file with "for now" a 32bit hex value 0x8FB4902F which I want to && with a mask 0xFF000000 then >> right shift 24 bits storing in result then printing the result. I thing a while or for loop is needed but I'm not quite sure how to go about it. How do I step through each character in this case and store it for use and passing to another function. ...
4
2745
by: ESPN Lover | last post by:
Below is two snippets of code from MSDN showing how to read a file. Is one way preferred over the other and why? Thanks. using System; using System.IO; class Test { public static void Main()
5
2295
by: Sridhar | last post by:
Hi, I have created a project which contains classes to read the data from the database. This project has an App.Config file which contains the SqlConnection String. when this code is called from a web application and if I need to read the connection string it is reading the connection string from the web.config of web application. I am not knowing how to change the Application domain so that it reads the App.config file instead of...
6
32917
by: Thomas Kowalski | last post by:
Hi, currently I am reading a huge (about 10-100 MB) text-file line by line using fstreams and getline. I wonder whether there is a faster way to read a file line by line (with std::string line). Is there some way to burst read the whole file and later "extract" each line? Thanks in advance, Thomas Kowalski
6
5716
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a * vector<string>. Now, use istringstream to read read each line * from the vector a word at a time.
5
11278
by: dm3281 | last post by:
Hello, I have a text report from a mainframe that I need to parse. The report has about a 2580 byte header that contains binary information (garbage for the most part); although there are a couple areas that have ASCII text that I need to extract. At the end of the 2580 bytes, I can read the report like a standard text file. It should have CR/LF at the end of each line. What is the best way for me to read this report using C#. It is...
2
5348
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi Is there any way to Read an INIFile from a string or Stream instead of a physical file ??? I want to read the INIFile into a string then store in a db but when I read the string from the db or save string to the db I don't want to have to copy the string to a file to use the WritePrivateProfileString and GetPrivateProfileString. Is there a way around this instead of writing my own class to operate on a string or stream ???
0
8989
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8828
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9537
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9243
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6795
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4599
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2213
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.