Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
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
Updated 28-Oct-21 19:20pm
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>");
#realJSOP 18-May-18 7:12am    
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
#realJSOP 18-May-18 8:24am    
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

1 solution

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:

C#
// 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.
 
Share this answer
 
v4

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900