Read a Xml (From a String) and Get Some Fields - Problems Reading Xml

Read a XML (from a string) and get some fields - Problems reading XML

You should use LoadXml method, not Load:

xmlDoc.LoadXml(myXML); 

Load method is trying to load xml from a file and LoadXml from a string. You could also use XPath:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);

string xpath = "myDataz/listS/sog";
var nodes = xmlDoc.SelectNodes(xpath);

foreach (XmlNode childrenNode in nodes)
{
HttpContext.Current.Response.Write(childrenNode.SelectSingleNode("//field1").Value);
}

How to parse xml and get the data from xml string?

You can parse Json easily than XML.

So I will suggest you to parse Json,

First Convert XMLto Json then parse the JsonObject.

here is reference you can take to convert XML to JSON Step by Step

https://stackoverflow.com/a/18339178/6676466

Can not read certain element from XML string

Here is what your code is missing. After

reader.ReadToFollowing("value");

You need to add the following line:

reader.Read();

After that it "reader.Value;" will return text inside <value>.
Alternatively instead of linq or Streamreader you can use DOM and xpath:

XmlDocument doc = new XmlDocument();
doc.Load("file.xml");
List<Rate> rates = new List<Rate>();

XmlNodeList nodes = doc.SelectNodes("//rate");

foreach(XmlNode x in nodes)
{
Rate r = new Rate();
r.Category = x.Attributes["category"].Value;
r.Date = DateTime.ParseExact(x.Attributes["date"].Value,"yyyy-MM-dd", null)x.Attributes["category"].Value;
r.Value = double.Parse(x.SelectSingleNode("./value").InnerText));
rates.Add(r);
}

Count how many times there is a specific text in a string and get the values in a array

You can use LINQ-to-XML to parse the string and obtain the values.

using System.Linq;
using System.Xml.Linq;

public static void Main()
{
var xml = @"<note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body><InputText>123</InputText><InputText>Apple</InputText><InputText>John</InputText></note>";

var list = XDocument.Parse(xml).Descendants("InputText").Select( x => x.Value );
foreach (var item in list) Console.WriteLine(item);
}

Output:

123
Apple
John

Fiddle

How to read this XML in C#?

You could do something like this: get the <ROWDATA> node, then its first (and only) descendant, and from that node, extract the info from the attributes with this code:

var field = xml.Root.Descendants("ROWDATA").Descendants().FirstOrDefault();

if (field != null)
{
string dateJoined = field.Attribute("dateJoined").Value;
decimal total = decimal.Parse(field.Attribute("totals").Value);
decimal partials = decimal.Parse(field.Attribute("partials").Value);

int status = int.Parse(field.Attribute("status").Value);
int counter = int.Parse(field.Attribute("counter").Value);
}

UPDATE: as @KlausGütter has noted in comments - if your .NET culture you're using uses something other than a dot (.) as its decimal separator, then this code won't properly convert the strings representing the decimal values.

To check what the decimal separator is, use this:

CultureInfo.CurrentCulture.Number.CurrencyDecimalSeparator

To properly convert the XML data to decimal in such a case, use:

    decimal total = (decimal)field.Attribute("totals");
decimal partials = (decimal)field.Attribute("partials");


Related Topics



Leave a reply



Submit