0

I know, that there are a lot of question about parsing C#, but I couldn't find answer. So, I need to write a DLL for parsing XML, but with some features, as I don't know what elements are in XML file. I need to parse all nodes of file and their elements. How can I do it? Now, I'm working with simple file

<reg>
    <email_login>[email protected]</email_login>
    <email_password>nDOUn3TybD</email_password>
</reg>

and my dll code now is

public XmlNodeList GetElementsName(string path)
{
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(path);
    XmlNodeList nodeList = xmlDoc.GetElementsByTagName("email_login");
    return nodeList;
}

It should return "[email protected]". My console app:

XMLWorker worker = new XMLWorker();
string path = "file:///D:/temp/test.xml";
XmlNodeList nodeList = worker.GetElementsName(path);
for (int i = 0; i < nodeList.Count; i++)
    Console.WriteLine(nodeList[i].InnerText);
Console.ReadLine();

But it returns "[email protected]"

How can I parse differently?

3
  • 1
    Also confirm what @azyberezovsky says, this works fine for me. The result is [email protected] by itself Commented Aug 13, 2013 at 14:08
  • 2
    That username/password looks pretty real... Commented Aug 13, 2013 at 14:10
  • @Henk Holterman, that username/password was generated by small app) But who knows...) Commented Aug 13, 2013 at 14:14

5 Answers 5

3

Use LINQ to XML:

XElement reg = XElement.Load(path);
string login = (string)reg.Element("email_login");

BTW your code works fine for me. Make sure you are not selecting all elements instead of email_login only. I.e. if you are getting child nodes XmlNodeList nodeList = xmlDoc.ChildNodes; instead of getting elements by tag name, then you will have your results.

Or possibly you have several elements named as email_login. E.g. following xml will produce your results with your code:

<reg>
  <email_login>[email protected]</email_login>
  <email_login>nDOUn3TybD</email_login>
</reg>
Sign up to request clarification or add additional context in comments.

Comments

0

i ran the exact same code you gave and got [email protected] as output so my guess is that you haven't build your project after you fixed something or it wasn't cleaned.

try to clean the project and run it again

Comments

0

You can do it this way

public List<String> getElementValues(string path,string elementName)
{
    XElement doc= XElement.Load(path);
    var elementList=doc.Descendants().Elements();
    return elementList.Where(x=>x.Name.LocalName==elementName)
                      .Select(y=>y.Value)
                      .ToList();
}

You can now get all the values of element with name email_login

var values=getElementValues(path,"email_login");

Comments

0

I ran following code after coping the XML data you provided in XMLFile.xml to emulate output:

class Program
{
    static void Main(string[] args)
    {
        XMLWorker worker = new XMLWorker();
        //
        string path = @"C:\Users\abc\Desktop\ConsoleApplication1\ConsoleApplication1\XMLFile.xml";
        XmlNodeList nodeList = worker.GetElementsName(path);
        for (int i = 0; i < nodeList.Count; i++)
        Console.WriteLine(nodeList[i].InnerText);
        Console.ReadLine();
    }       
}

public class XMLWorker
{
    public XmlNodeList GetElementsName(string path)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(path);
        XmlNodeList nodeList = xmlDoc.GetElementsByTagName("email_login");

        return nodeList;
    }
}

But for me it is working fine.

2 Comments

That was already said by me, by NoIdeaForName, and by SwDevMan81 in comments
Yes...now lets see what we get here causing the issue !!
0

You can also use XPath query:

XmlNodesList nodesList = xmlDoc.SelectNodes("//email_login"));
foreach(string oneNode in nodesList) 
{
    Console.Write(oneNode.InnerText);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.