XML and C# - Help pulling information from a certain element
So I don't know how to exactly word it, but here it goes. Right now my Application can pull the information about the User's from the XML without problem. Sample XML:
<statuses type="array">
<status>
<id>ID1</id>
<text>Text That I want to pull 1</text>
<user>
<name>User1</name>
<screen_name>User1_Screen</screen_name>
<location>Location_User1</location>
<Description>User1_Description</Description>
</user>
</status>
<status>
<id>ID2</id>
<text>Text That I want to pull 2</text>
<user>
<name>User2</name>
<screen_name>User2_Screen</screen_name>
<location>Location_User2</location>
<Description>User2_Description</Description>
</user>
</status>
Which the Screen_name tag gets pulled into a listbox. And here's the code that pulls the information in which it gets user's detail's:
private void listBox1_SelectedValueChanged(object sender, EventArgs e)
{
//Load the temp file
var doc = XDocument.Load("Temp.xml");
var setting = doc.Descendants("user").First(a => a.Ele开发者_高级运维ment("screen_name").Value == listBox1.Text);
//Variables for information from XML
var Location = setting.Element("location").Value;
var Description = setting.Element("description").Value;
var Screen_name = setting.Element("name").Value;
//Info Pulled from "temp.xml"
location.Text = "Location: "+Location;
Username.Text = "Name: " + Screen_name;
descriptionBox.Text = "Description: "+Description;
}
and All i want now is the text tag also, and I'm just having a terrible time figuring it out.
I ran your code without any problems. You should to check:
- Is your
listBox1.Text
value correct? - Are you trying to acess that
Temp.xml
in correct path? - Are you sure your
Temp.xml
is well-formed (it's missing a</statuses>
); Try to open it into your IE.
EDIT: Try this:
var text = setting.Parent.Element("text").Value;
精彩评论