Answered by:
How do I get the values of attributes

Question
-
This is the xml format I have
<mission_script> <briefing text_id="01" map_texture="/data/textures/atlas_gui/mission_gfx/briefing_map_m01"> <briefing_text txt_id="briefing_m01_strategic" headline_id="briefing_strategic_hl" anchor="obj1" pos="0.074 0.246 -2" type="objective" /> <map_text txt_id="loc_sp_mission01_01" pos="0.67 0.02 0.4" type="small" /> <actor name="m01_briefing" /> <video name="data/movies/m01_video.bik" /> </briefing> </mission_script>
And this is the C# code I have to collect the values
xDoc.Load(path + "/Graw Mission Script/mission.xml"); Mission p = new Mission();
foreach (XmlNode xNode in xDoc.SelectNodes("mission_script/briefing")) { p.Atrib1 = xNode.Attributes["text_id"].InnerText; p.Atrib2 = xNode.Attributes["map_texture"].InnerText; } foreach (XmlNode xNode in xDoc.SelectNodes("mission_script/briefing/briefing_text")) { p.Atrib3 = xNode.Attributes["txt_id"].InnerText; p.Atrib4 = xNode.Attributes["headline_id"].InnerText; p.Atrib5 = xNode.Attributes["anchor"].InnerText; p.Atrib6 = xNode.Attributes["pos"].InnerText; p.Atrib7 = xNode.Attributes["type"].InnerText; } foreach (XmlNode xNode in xDoc.SelectNodes("mission_script/briefing/map_text")) { p.Atrib8 = xNode.Attributes["txt_id"].InnerText; p.Atrib9 = xNode.Attributes["pos"].InnerText; p.Atrib10 = xNode.Attributes["type"].InnerText; } foreach (XmlNode xNode in xDoc.SelectNodes("mission_script/briefing/actor")) { p.Atrib11 = xNode.Attributes["name"].InnerText; } foreach (XmlNode xNode in xDoc.SelectNodes("mission_script/briefing/video")) { p.Atrib12 = xNode.Attributes["name"].InnerText; } nodes.Add(p); listView1.Items.Add(p.Atrib1);
Any help with it would be great
Thank you
- Edited by TDHarr Thursday, February 5, 2015 11:07 PM
Thursday, February 5, 2015 11:04 PM
Answers
-
Hi TDHarr,
I think you could use LINQ TO XML and I think you could try the code below:
var xDoc = System.Xml.Linq.XDocument.Load(@"D:\Test\VSC#\02\Winfrom\WindowsFormsApplication1\XMLFile1.xml"); var items = from i in xDoc.Descendants("briefing") select new { Action = (string)i.Attribute("text_id"), FileName = (string)i.Attribute("map_texture") }; foreach (var item in items) { MessageBox.Show(item.Action); MessageBox.Show(item.FileName); // use item.Action or item.FileName }
You could get more information about linq to xml from the link below:
# LINQ to XML Overview
https://msdn.microsoft.com/en-us/library/bb387061.aspxIf you have any further questions, please feel free to post in this forum.
Best Regards,
Edward
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.Friday, February 6, 2015 11:20 AM -
.InnerText usualy returns the [text] contents of an Element.
Use .Value to get the .. er .. value of an Attribute.
Remember that SelectNodes can return more than one Node! If you only mean to retrieve one, use SelectSingleNode - it makes your code a lot clearer to read.
XmlNode node = xDoc.SelectSingleNode( " . / briefing " ); if ( null != node ) { p.Atrib1 = node.GetAttribute( "text_id" ); p.Atrib2 = node.GetAttribute( "map_texture" ); XmlNode node2 = node.SelectSingleNode( " briefing_text " ); if ( null != node2 ) { p.Atrib3 = node2.GetAttribute( "txt_id" ); p.Atrib4 = node2.GetAttribute( "headline_id" ); p.Atrib5 = node2.GetAttribute( "anchor" ); p.Atrib6 = node2.GetAttribute( "pos" ); p.Atrib7 = node2.GetAttribute( "type" ); } node2 = node.SelectSingleNode( " map_text " ); if ( null != node2 ) { p.Atrib8 = node2.GetAttribute( "txt_id" ); p.Atrib9 = node2.GetAttribute( "pos" ); p.Atrib10 = node2.GetAttribute( "type" ); } node2 = node.SelectSingleNode( " actor " ); if ( null != node ) { p.Atrib11 = node2.GetAttribute( "name" ) ; } node2 = node.SelectSingleNode( " video " ); if ( null != node ) { p.Atrib11 = node2.GetAttribute( "name" ) ; } }
Regards, Phill W.
Friday, February 6, 2015 12:21 PM
All replies
-
Hi TDHarr,
I think you could use LINQ TO XML and I think you could try the code below:
var xDoc = System.Xml.Linq.XDocument.Load(@"D:\Test\VSC#\02\Winfrom\WindowsFormsApplication1\XMLFile1.xml"); var items = from i in xDoc.Descendants("briefing") select new { Action = (string)i.Attribute("text_id"), FileName = (string)i.Attribute("map_texture") }; foreach (var item in items) { MessageBox.Show(item.Action); MessageBox.Show(item.FileName); // use item.Action or item.FileName }
You could get more information about linq to xml from the link below:
# LINQ to XML Overview
https://msdn.microsoft.com/en-us/library/bb387061.aspxIf you have any further questions, please feel free to post in this forum.
Best Regards,
Edward
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.Friday, February 6, 2015 11:20 AM -
.InnerText usualy returns the [text] contents of an Element.
Use .Value to get the .. er .. value of an Attribute.
Remember that SelectNodes can return more than one Node! If you only mean to retrieve one, use SelectSingleNode - it makes your code a lot clearer to read.
XmlNode node = xDoc.SelectSingleNode( " . / briefing " ); if ( null != node ) { p.Atrib1 = node.GetAttribute( "text_id" ); p.Atrib2 = node.GetAttribute( "map_texture" ); XmlNode node2 = node.SelectSingleNode( " briefing_text " ); if ( null != node2 ) { p.Atrib3 = node2.GetAttribute( "txt_id" ); p.Atrib4 = node2.GetAttribute( "headline_id" ); p.Atrib5 = node2.GetAttribute( "anchor" ); p.Atrib6 = node2.GetAttribute( "pos" ); p.Atrib7 = node2.GetAttribute( "type" ); } node2 = node.SelectSingleNode( " map_text " ); if ( null != node2 ) { p.Atrib8 = node2.GetAttribute( "txt_id" ); p.Atrib9 = node2.GetAttribute( "pos" ); p.Atrib10 = node2.GetAttribute( "type" ); } node2 = node.SelectSingleNode( " actor " ); if ( null != node ) { p.Atrib11 = node2.GetAttribute( "name" ) ; } node2 = node.SelectSingleNode( " video " ); if ( null != node ) { p.Atrib11 = node2.GetAttribute( "name" ) ; } }
Regards, Phill W.
Friday, February 6, 2015 12:21 PM -
What I'm trying to do is get the values of the attributes to load back into
the text boxes when I click on the name in the list view
I can give you all the code I have so far, but it would be to much to put here
So here is a link to the source
I have uploaded the project to One Drive
and as far as the link above, you just have to answer a
question to download it. I just tested the link and it
works fine.
- Edited by TDHarr Monday, February 9, 2015 7:46 PM
Friday, February 6, 2015 8:02 PM -
Hi TDHarr,
I turned to the link, but I could not open the file. Could you share the file in OneDrive?
Regards,
Tony
Help each other
Saturday, February 7, 2015 6:33 AM -
I miss a little bit the relation in your question with the purpose of this forum.
Questions around apis, controls, classes, components around the user interface Windows forms.
It looks to me more a question for the C# forum were persons are more dedicated to your problem.
XML is for instance one of the parts where C# has not complete parity with VB.
https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=csharpgeneral
Success
CorMonday, February 9, 2015 9:20 AM -
Hi TDHarr,
I made a test with the replies from Edward and Phill, it could get the attribute of the nodes.
Could you share us what your issue is?
I could not download your project also.
Best Regards,
Tony
Help each other
Monday, February 9, 2015 11:27 AM -
I'm sorry, But I don't know who marked this as answerd
but it's not, I would like an answer that works
or I'll report the people that marked it.
thank you
Friday, February 20, 2015 4:10 PM -
Hi TDHarr,
As the reply from Phill, if you want to get the value of the nodes, you could use the .Value instead of the .InnerText.
>>What I'm trying to do is get the values of the attributes to load back into the text boxes when I click on the name in the list view
With the .Value, you could get all the values of the attributes, and if you want to set the value to text boxes, I think you could get one values and set one value, also, you could store them in a list, and set the list to the textboxes.
If your issue has not been resolved, you could share us you key code which you got the values of the attributes and set the values of the textbox.
Best Regards,
Edward
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.Wednesday, February 25, 2015 5:18 AM