Click here to Skip to main content
15,909,503 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have xml file structure like,
XML
<?xml version="1.0" encoding="utf-16"?>
<root>
  <path>C:\Users\bsushil\Documents\Sharpdesk Desktop\New folder</path>
  <path>C:\Users\bsushil\Documents\Sharpdesk Desktop\New folder (1)</path>
  <path>C:\Users\bsushil\Documents\Sharpdesk Desktop\New folder (2)</path>
</root>


How do i retrieve the Path value.

What I have tried:

C#
XDocument xdoc = XDocument.Load(@"D:\Sushil\Information.xml");

           var servers = xdoc.Descendants("root");
           for (var server in servers)
Posted
Updated 8-Feb-18 20:27pm

1 solution

You're on the right track:

C#
XDocument xdoc = XDocument.Load(@"D:\Sushil\Information.xml");
var paths = xdoc.Descendants("path").Select(x=>x.Value).ToList();
for (var p in paths)
{
    Console.WriteLine("{0}", p);
}
 
Share this answer
 
v2
Comments
Member 11859517 16-Feb-18 8:32am    
Thanks Maciej,

I got the path, now i am checking whether the directory is exist or not, if not exist then immediately I have to remove that node from xml file, how can we do this?
Maciej Los 17-Feb-18 8:49am    
You're very welcome.
Check this: XNode.Remove Method ()
Member 11859517 20-Feb-18 23:56pm    
Thanks Maciej Los,
I am not able to delete from the xml file, You gave solution for getting the path

var paths = xdoc.Descendants("path").Select(x=>x.Value).ToList();
for (var p in paths)
{
if(!Directory.Exist(p)
{
remove from the xml????? how can I do this,
}
}
Maciej Los 21-Feb-18 2:02am    
p.Remove();
Member 11859517 21-Feb-18 4:16am    
Getting this error,
Error 104 The type 'char' cannot be used as type parameter 'T' in the generic type or method
'System.Xml.Linq.Extensions.Remove<t>(System.Collections.Generic.IEnumerable<t>)'.
There is no boxing conversion from 'char' to 'System.Xml.Linq.XNode'.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900