Answers for "xml reader c# example"

C#
3

How to read a XML on C#

XmlDocument doc = new XmlDocument();
            doc.Load(path);
            doc.Save(Console.Out);

            foreach (XmlNode node in doc.DocumentElement)
            {
                string word_name = node.Attributes[0].Value;
                string word_translation = node["name of node"].InnerText;
            }
Posted by: Guest on February-12-2021
2

read xml file c#

XmlDocument doc = new XmlDocument();
using (StreamReader streamReader = new StreamReader(path_name, Encoding.UTF8))
{
	contents = streamReader.ReadToEnd();
}
doc.LoadXml(contents);
Posted by: Guest on June-11-2020
0

xml reader attributes

// Get Xml node attribute keys and values
static IEnumerable<(string key, string value)> GetNodeAttributes(XmlReader reader)
{
    for (int i = 0; i < reader.AttributeCount; i++)
    {
        reader.MoveToAttribute(i);
        yield return (reader.Name, reader.Value);
    }
}

// This can be used with a Linq selector
//to retrieve specific value from the attributes
static string GetAttributeValue(this IEnumerable<(string key, string value)> attributes, string attribute) =>
    attributes.Where(x => x.key == attribute).ToList()[0].value;

// Combine these methods like so to get a attribute neatly.
string value = GetNodeAttributes(reader).GetAttributeValue("name");
Posted by: Guest on September-07-2021

C# Answers by Framework

Browse Popular Code Answers by Language