Click here to Skip to main content
13,793,019 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
We have to read xml string with particular tag name. Please advise ASAP
we have to use some code for single root value, but we need multiple root elements and particular tag name to read data.
Here is my code we have to use single root value its working fine,

What I have tried:

string xmlString = "<date>5/15/2018<name>test test<course>demo";

XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlString);
XmlNodeList nodes = doc.DocumentElement.SelectNodes("//data");
StringBuilder yourString = new StringBuilder();
foreach (XmlNode node in nodes)
{
yourString.Append("Date:" + node["Date"].InnerText + "
");
yourString.Append("CoachName:" + node["CoachName"].InnerText + ",");
}
PlaceHolder1.Controls.Add(new LiteralControl(yourString.ToString()));
Posted 18-May-18 1:24am
Updated 18-May-18 2:17am
Comments
F-ES Sitecore 18-May-18 6:27am
   
If you need multiple roots, it's a bit hacky but you can wrap your xml in an artificial root

string xmlString = "<date>5/15/2018</date><date>5/16/2018</date>";
XmlDocument doc = new XmlDocument();
doc.LoadXml("<root>" + xmlString + "</root>");
   
If you had posted this as an answer, I could vote it a 5. :)
Member 12523149 18-May-18 7:27am
   
Thanks for your quick replay
i have used your code its working with static content, but dynamic content not working. please advise
   
What do you mean by "dynamic content"?
Member 12523149 18-May-18 8:31am
   
Xml data are stored in table, using stored procedure to get data
   
You mean multiple rows returned, right?
   
The data itself is stored in xml format (as a single giant string), or you're using the SQL Server FOR XML AUTO,ELEMENTS clause to get the data from the table
   
I updated my solution (below).

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

First, your xmlString variable is malformed - you need closing tags for each element.

To resolve the multiple roots exception, do what fes-sitecore said. I use a similar, yet alternative construct:

// your xml string, corrected by adding closing tags
string xmlString = "<date>5/15/2018</date><name>test test</name><course>demo</course>";

// just parse the xml string after adding a root wrapper tag.
XElement xml = XElement.Parse(string.Format("<root>{0}</root>",xmlString));

// now you can retrieve the elements:
string date = xml.Element("date").Value;
string name = xml.Element("name").Value;
string course = xml.Element("course").Value;


EDIT =================================

You say that it doesn't work with "dynamic content", which you defined as "retrieved from the database". Either the data is stored as one giant XML string (BAD idea), or you're using the SQL Server FOR XML AUTO,ELEMENTS clause to retrieve the data.

If it's the first possibility (which is also probably the case), you have to process each row one at a time, and wrap the data from each row with an appropriate root element ("row" would be a good element name), and then the combine the rows into a single XML string, which is then wrapped with an appropriate root element.

If it's the 2nd possibility, simply wrap the returned XML string with a root element, and process its child elements, which should all have the same element name.

Either way, it would then be a simple matter to parse/deserialize your data into C# objects.
  Permalink  
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web03 | 2.8.181207.3 | Last Updated 18 May 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100