1

My c# program retrieves an xml data column from my db containing a path to a text file as follwows

<path>
  <path name="myfile" url="/test/dir/YUUHGGGVFY/grgrggr.text" />
</path>

So the above is stored in a string variable name = pathstring

How can I format the above string to only extract the "/test/dir/YUUHGGGVFY/grgrggr.text" portion ?

The other sections of the string will always be the same:

so pathstring = "/test/dir/YUUHGGGVFY/grgrggr.text" portion ?

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
user1526912
  • 15,818
  • 14
  • 57
  • 92
  • See this SO question: [Best practices to parse xml files?](http://stackoverflow.com/questions/55828/best-practices-to-parse-xml-files) – sinelaw Jan 10 '13 at 21:13

2 Answers2

3

You can use Linq to Xml to parse your string and get url attribute from path

string xml = 
  @"<path>
       <path name=""myfile"" url=""/test/dir/YUUHGGGVFY/grgrggr.text"" />
    </path>";
XElement pathElement = XElement.Parse(xml);
var pathString = (string)pathElement.Element("path").Attribute("url");
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
0

Take a look at LINQ2XML. I'll just provide you with a working solution for that particular use case:

string path = 
  @"<path>
      <path name=""myfile"" url=""/test/dir/YUUHGGGVFY/grgrggr.text"" />
    </path>";
XDocument xdoc = XDocument.Parse(path);
var pathString = (string)xdoc.Element("path").Element("path").Attribute("url");
Claudio Bernasconi
  • 158
  • 1
  • 1
  • 13