M
M
Max Khrichtchatyi2014-05-16 14:53:44
.NET
Max Khrichtchatyi, 2014-05-16 14:53:44

C# XML file parsing

I have a file with content:

<?xml version="1.0" encoding="UTF-8"?>
<resources>
<!-- API errors -->
<string name="705">Введите адрес эл. почты</string>
<string name="706">Неверно указан адрес эл. почты</string>
<string name="707">Введите пароль</string>
<string name="708">Неверно указан пароль</string>  
</resources>

the task is to make a function that receives NAME = 705, the output should be "Enter an e-mail address".
Now I'm trying to reach name, but I get: "The resource name '705' is not a valid identifier."

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
aush, 2014-05-16
@j0shu4b0y

Add your own error handling.

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

namespace ConsoleApplication11
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var xml = XDocument.Load("XMLFile1.xml");

            Console.WriteLine(GetErrorText(706, xml));
            Console.ReadLine();
        }

        private static string GetErrorText(int name, XDocument xDoc)
        {
            return xDoc.Root.Elements().First(el => int.Parse(el.Attribute("name").Value) == name).Value;
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question