且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从具有特定标记的字符串变量中读取XML

更新时间:2022-10-15 09:45:25

首先,你的 xml字符串变量格式错误 - 您需要为每个元素关闭标记。



要解决多根异常,请执行 fes-sitecore 所说的内容。我使用了类似的替代构造:



  //  您的xml字符串,通过添加结束标记进行更正 
string xmlString = < date> 5/15/208< / date>< name> test test< / name>< course> demo< / course> 跨度>;

// 只需在添加根包装标记后解析xml字符串。
XElement xml = XElement.Parse( string .Format( &LT;根&GT; {0}&LT; /根> 中跨度>,的xmlString));

// 现在您可以检索元素:
string
date = xml.Element( date)值;
string name = xml.Element( 名称跨度>)值。
string course = xml.Element( 当然)。价值;





编辑================ =================



您说它不适用于您定义的动态内容 as从数据库中检索。数据存储为一个巨大的XML字符串(BAD构思),或者您使用SQL Server FOR XML AUTO,ELEMENTS 子句来检索数据。



如果这是第一种可能性(也可能是这种情况),你必须一次处理一行,并用每行包装数据。适当的根元素(row将是一个好的元素名称),然后将行组合成一个XML字符串,然后用适当的根元素包装。



如果它是第二种可能性,只需用返回的根元素包装返回的XML字符串,并处理其子元素,它们都应具有相同的元素名称。



无论哪种方式,将数据解析/反序列化为C#对象都是一件简单的事情。


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

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.