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 ?

1

2 Answers 2

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");
Sign up to request clarification or add additional context in comments.

1 Comment

@sixlettervariables thanks I was thinking that I missed something and yes, it was referencing Root element
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");

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.