Suppose we have read the following XML structure in a string.
Write a program that can print only the non-empty employee names.
<EmpRecord> <Employee1> <EmpName>R</EmpName> <EmpId>1001</EmpId> </Employee1> <Employee2> <EmpName>A</EmpName> <EmpId>1002</EmpId> </Employee2> <Employee3> <EmpName></EmpName> <EmpId>1003</EmpId> </Employee3> <Employee4> <EmpName>S</EmpName> <EmpId>1004</EmpId> </Employee4> </EmpRecord>
Split string and then check the value
using System; using System.Collections.Generic; class Program//w ww . j a va 2 s .c om { static void Main(string[] args) { string employee =@"<EmpRecord> <Employee1> <EmpName>R</EmpName> <EmpId>1001</EmpId> </Employee1> <Employee2> <EmpName>A</EmpName> <EmpId>1002</EmpId> </Employee2> <Employee3> <EmpName></EmpName> <EmpId>1003</EmpId> </Employee3> <Employee4> <EmpName>S</EmpName> <EmpId>1004</EmpId> </Employee4> </EmpRecord>"; string[] splt = { "<EmpName>", "</EmpName>" }; List<string> empNamesList = new List<string>(); string[] temp = employee.Split(splt, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < temp.Length; i++) { if (!temp[i].Contains("<")) { empNamesList.Add(temp[i]); } } foreach (string s in empNamesList) { Console.WriteLine(s); } } }