Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Sunday, September 24, 2017 2:11 PM
Say, I have a xml file which look like below
<?xml version="1.0" encoding="utf-8" ?>
<Employees>
<Employee>
<EmpId>1</EmpId>
<Name>Sam</Name>
<Sex>Male</Sex>
<Phone Type="Home">423-555-0124</Phone>
<Phone Type="Work">424-555-0545</Phone>
<Address>
<Street>7A Cox Street</Street>
<City>Acampo</City>
<State>CA</State>
<Zip>95220</Zip>
<Country>USA</Country>
</Address>
</Employee>
<Employee>
<EmpId>2</EmpId>
<Name>Lucy</Name>
<Sex>Female</Sex>
<Phone Type="Home">143-555-0763</Phone>
<Phone Type="Work">434-555-0567</Phone>
<Address>
<Street>Jess Bay</Street>
<City>Alta</City>
<State>CA</State>
<Zip>95701</Zip>
<Country>USA</Country>
</Address>
</Employee>
<Employee>
<EmpId>3</EmpId>
<Name>Kate</Name>
<Sex>Female</Sex>
<Phone Type="Home">166-555-0231</Phone>
<Phone Type="Work">233-555-0442</Phone>
<Address>
<Street>23 Boxen Street</Street>
<City>Milford</City>
<State>CA</State>
<Zip>96121</Zip>
<Country>USA</Country>
</Address>
</Employee>
</Employees>
How do I get contents inside the first <Name> node or the second one?
I've tried
XDocument doc = XDocument.Load(@"C:\Users\Desktop\test.xml");
string name1=doc.Element("Name").Value;
textBox1.Text=name1;
But it given an error System.NullReferenceException: Object reference not set to an instance of an object.
All replies (3)
Monday, September 25, 2017 2:08 AM âś…Answered | 1 vote
Hello Don Bradman,
>>is there a way to get the content of a node by name which might a child node to different nodes or do I have to always specify what are the parent nodes of the node whose content I want to use
yes, if you want to get node value by node name directly , you could try to use Descendants.
var result1 =xDocument.Descendants("Name").Select(s=> s.Value);
>>and also how do I get, say the 2nd <Name> node content i.e. "Lucy"?
You should choose the node as sign that specify the 2nd node . Something like below.
var result = from s in xDocument.Descendants("Employee")
where s.Element("EmpId").Value == "2"
select s.Element("Name").Value;
Sincerely,
feih_7
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].
Sunday, September 24, 2017 3:10 PM | 1 vote
To get the first name:
string name1 = doc.Root.Element( "Employee" ).Element( "Name" ).Value;
To access all of the names in a loop:
foreach( var node in doc.Root.Elements( "Employee" ) )
{
string name = node.Element( "Name" ).Value;
// . . .
}
To get a collection of names using XPath and LINQ:
using System.Xml.XPath;
. . .
var names = doc.XPathSelectElements( "/*/Employee/Name" ).Select( n => n.Value );
Sunday, September 24, 2017 4:01 PM
Hi Viorel,
Thank you for the answer.
BTW, is there a way to get the content of a node by name which might a child node to different nodes or do I have to always specify what are the parent nodes of the node whose content I want to use? and also how do I get, say the 2nd <Name> node content i.e. "Lucy"?