C# Question

How to read an node's content from an Xml file to a string in c# 2.0?

I have this Xml file:

<Element>
<Name>Startlap</Name>
<ToolTip>Magyarország legnagyobb internetes portálja</ToolTip>
<Action>OpenWebPage</Action>
<ActionParam1>http://www.startlap.hu</ActionParam1>
<ActionParam2>default</ActionParam2>
<ActionParam3>false</ActionParam3>
<ImageOnDisk>false</ImageOnDisk>
<ImageOnline>http://www.pro-qaly.hu/files/userfiles/logo-startlap.jpg</ImageOnline>

<Name>secondElement</Name>
<ToolTip>Magyarország legnagyobb internetes portálja</ToolTip>
<Action>OpenWebPage</Action>
<ActionParam1>http://www.startlap.hu</ActionParam1>
<ActionParam2>default</ActionParam2>
<ActionParam3>false</ActionParam3>
<ImageOnDisk>false</ImageOnDisk>
<ImageOnline>http://www.pro-qaly.hu/files/userfiles/logo-startlap.jpg</ImageOnline>




How can I save the first and the second name attribute in c# to their own variables?

Answer Source

You can use XmlDocument class as follows:

XmlDocument doc = new XmlDocument();
doc.LoadXml(yourXml);
XmlNodeList elements = doc.SelectNodes("//Element/Name");
string name1 = elements[0].InnerText;
string name2 = elements[1].InnerText;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download