Read XML from string instead of file (C#)

T

th3dude

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...
 
P

PeterKellner

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
 
T

th3dude

Peter,

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

Cheers.
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
 
T

th3dude

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!

Peter,

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

Cheers.
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
 
R

Remy

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
 
T

th3dude

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top