Click here to Skip to main content
15,905,325 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a xml like the following example, which is also used in XML Data Source. I don't want to create a new xml, rather than use this one.

XML
<App>
  <Post id="1" text="Hello"></Post>
  <Post id="2" text="Hello1"></Post>
  <Post id="3" text="Hello2"></Post>
</App>


Now I want to create a c# function to match a "string" with the "id" attribute and return the "text". I have created a function (below) but unable to solve it. Please Help. Thanks in Advance.

C#
public string GetText(string id)
{
  string selected = sid;
  XDocument doc = XDocument.Load(HttpContext.Current.Server.MapPath("application.xml"));
}
Posted

Try this


C#
public string GetText(string id)
{
  string selected = sid;
  XDocument doc = XDocument.Load(HttpContext.Current.Server.MapPath("application.xml"));
  sid=doc.SelectSingleNode("/App/Post[id=" + id + "]").Value
}
 
Share this answer
 
v2
Try this

C#
public string GetText(string id)
{
  string selected = sid;
  XDocument doc = XDocument.Load(HttpContext.Current.Server.MapPath("application.xml"));
  string sid = doc.SelectSingleNode("//Post[@id='" + id + "']").Attributes["text"].Value;
}
 
Share this answer
 

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