By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,740 Members | 844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,740 IT Pros & Developers. It's quick & easy.

Read XML from string instead of file (C#)

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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<MembershipProfileInfo>
CheckXMLForProfileData(string xmlData,string userName,out
string errorString)
{
// take in this xml and look for profile data
// <profile defaultProvider="SqlProfileProvider">
// <providers>
// <remove name="AspNetSqlProfileProvider"/>
// <add name="SqlProfileProvider"
type="System.Web.Profile.SqlProfileProvider"
// connectionStringName="LocalSqlServer"/>
// </providers>
// <properties>
// <add name="advancedMode" type="bool"/>
// <add name="defaultConnectionName"
type="string"/>
// <add name="defaultNameSpaceGenerated"
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.ASCIIEncoding encoding = new
System.Text.ASCIIEncoding();
byteArray = encoding.GetBytes(xmlData);

// Load the memory stream
MemoryStream memoryStream = new MemoryStream(byteArray);
//XmlDocument doc = new XmlDocument();
memoryStream.Seek(0, SeekOrigin.Begin);

List<MembershipProfileInfo> mpiLi = new
List<MembershipProfileInfo>();

if (byteArray.Length > 0)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(memoryStream);

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

foreach (XmlNode nodex in nodeList)
{
if (nodex.HasChildNodes)
{
foreach (XmlNode nodexx in
nodex.ChildNodes)
{
if (nodexx.Name.Equals("group"))
{
// Get Group Name
MembershipProfileInfo mpi = new
MembershipProfileInfo();
mpi = GetAttrs(nodexx,
string.Empty);
string prefixName = mpi.Name;
foreach (XmlNode nodexxx in
nodexx.ChildNodes)
{
MembershipProfileInfo mpi1 =
new MembershipProfileInfo();
mpi1 = GetAttrs(nodexxx,
prefixName);
mpiLi.Add(mpi1);
}
}
else if (nodexx.Name.Equals("add"))
{
MembershipProfileInfo mpi = new
MembershipProfileInfo();
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

P: n/a
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<MembershipProfileInfo>
CheckXMLForProfileData(string xmlData,string userName,out
string errorString)
{
// take in this xml and look for profile data
// <profile defaultProvider="SqlProfileProvider">
// <providers>
// <remove name="AspNetSqlProfileProvider"/>
// <add name="SqlProfileProvider"
type="System.Web.Profile.SqlProfileProvider"
// connectionStringName="LocalSqlServer"/>
// </providers>
// <properties>
// <add name="advancedMode" type="bool"/>
// <add name="defaultConnectionName"
type="string"/>
// <add name="defaultNameSpaceGenerated"
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.ASCIIEncoding encoding = new
System.Text.ASCIIEncoding();
byteArray = encoding.GetBytes(xmlData);

// Load the memory stream
MemoryStream memoryStream = new MemoryStream(byteArray);
//XmlDocument doc = new XmlDocument();
memoryStream.Seek(0, SeekOrigin.Begin);

List<MembershipProfileInfo> mpiLi = new
List<MembershipProfileInfo>();

if (byteArray.Length > 0)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(memoryStream);

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

foreach (XmlNode nodex in nodeList)
{
if (nodex.HasChildNodes)
{
foreach (XmlNode nodexx in
nodex.ChildNodes)
{
if (nodexx.Name.Equals("group"))
{
// Get Group Name
MembershipProfileInfo mpi = new
MembershipProfileInfo();
mpi = GetAttrs(nodexx,
string.Empty);
string prefixName = mpi.Name;
foreach (XmlNode nodexxx in
nodexx.ChildNodes)
{
MembershipProfileInfo mpi1 =
new MembershipProfileInfo();
mpi1 = GetAttrs(nodexxx,
prefixName);
mpiLi.Add(mpi1);
}
}
else if (nodexx.Name.Equals("add"))
{
MembershipProfileInfo mpi = new
MembershipProfileInfo();
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

P: n/a
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<MembershipProfileInfo>
CheckXMLForProfileData(string xmlData,string userName,out
string errorString)
{
// take in this xml and look for profile data
// <profile defaultProvider="SqlProfileProvider">
// <providers>
// <remove name="AspNetSqlProfileProvider"/>
// <add name="SqlProfileProvider"
type="System.Web.Profile.SqlProfileProvider"
// connectionStringName="LocalSqlServer"/>
// </providers>
// <properties>
// <add name="advancedMode" type="bool"/>
// <add name="defaultConnectionName"
type="string"/>
// <add name="defaultNameSpaceGenerated"
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.ASCIIEncoding encoding = new
System.Text.ASCIIEncoding();
byteArray = encoding.GetBytes(xmlData);

// Load the memory stream
MemoryStream memoryStream = new MemoryStream(byteArray);
//XmlDocument doc = new XmlDocument();
memoryStream.Seek(0, SeekOrigin.Begin);

List<MembershipProfileInfo> mpiLi = new
List<MembershipProfileInfo>();

if (byteArray.Length > 0)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(memoryStream);

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

foreach (XmlNode nodex in nodeList)
{
if (nodex.HasChildNodes)
{
foreach (XmlNode nodexx in
nodex.ChildNodes)
{
if (nodexx.Name.Equals("group"))
{
// Get Group Name
MembershipProfileInfo mpi = new
MembershipProfileInfo();
mpi = GetAttrs(nodexx,
string.Empty);
string prefixName = mpi.Name;
foreach (XmlNode nodexxx in
nodexx.ChildNodes)
{
MembershipProfileInfo mpi1 =
new MembershipProfileInfo();
mpi1 = GetAttrs(nodexxx,
prefixName);
mpiLi.Add(mpi1);
}
}
else if (nodexx.Name.Equals("add"))
{
MembershipProfileInfo mpi = new
MembershipProfileInfo();
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

P: n/a
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(xml));

Cheers

Remy

Jun 5 '06 #5

P: n/a
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(xmlstring);

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(xml));

Cheers

Remy


Jun 5 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.