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 XML
to Jso
n 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
How to Apply a General Rule for Remapping All Property Names When Serializing with JSON.Net
Best Way to Share Data Between Two Child Components in Blazor
Copying Free Hand Drawing from Panel in Visual Studio 2013
How to Fix the Error:"Unreachable Code Detected"
Blocking Access to Private Member Variables? Force Use of Public Properties
Why Are There System.*.* Dlls in My Bin Release Directory
How to Highlight Wrapped Text in a Control Using the Graphics
What Is the Default Value for Enum Variable
How to Make Form1 Label.Text Change When Checkbox on Form2 Is Checked
Auto-Resize Multiple Forms Rendered on Panel
Initializing an Array of Structs in C#
Newtonsoft Inline Formatting for Subelement While Serializing
Additional Setup in Visual Studio Installer Projects
Resources for Learning C# Excel Interop
In C# What Category Does the Colon ":" Fall Into, and What Does It Really Mean