Reputation: 2724
First off, I'm sorry for the name. I couldn't think of a way to describe my issue in a question form. But this is what I'm trying to do.
Here is what my xml is looking like:
<Settings>
<Display_Settings>
<Screen>
<Name Name="Screen" />
<ScreenTag Tag="Screen Tag" />
<LocalPosition X="12" Y="81" Z="28" />
<Width Width="54" />
<Height Height="912" />
</Screen>
<Camera_Name Name="Camera">
<CameraTag Tag="Camera Tag" />
<LocalPosition X="354" Y="108" Z="Z Local Position" />
<Far Far="98" />
<Near Near="16" />
<FOV FOV="78" />
<AspectRatio AspectRatio="1" />
<ScreenDistance ScreenDistance="2" />
</Camera_Name>
</Display_Settings>
</Settings>
What I want is to access the attribute values stored within my local position node. I got some help with this and I can access the screens local position attribute value with this code:
var xdoc = XDocument.Load("C:\\Test.xml");
int x = int)xdoc.Descendants("LocalPosition").First().Attribute("X");
This happily returns 12 when I debug it. But, I also want to my cameras local position to be out putted as well.
Can someone please show me how to do this?
Upvotes: 0
Views: 394
Reputation: 23646
You can grab Camera and Screen position using Descendants
and then accessing it components with Attribute
. Code examples are given below:
var cameraPosition = xdoc.Descendants("Camera_Name")
.First()
.Element("LocalPosition");
var screenPosition = xdoc.Descendants("Screen")
.First()
.Element("LocalPosition");
//parsing and displaying data
int cameraX = int.Parse(cameraPosition.Attribute("X").Value);
int cameraY = int.Parse(cameraPosition.Attribute("Y").Value);
Console.WriteLine ("camera pos: X={0}; Y={1}", cameraX, cameraY);
int screenX = int.Parse(screenPosition.Attribute("X").Value);
int screenY = int.Parse(screenPosition.Attribute("Y").Value);
Console.WriteLine ("screen pos: X={0}; Y={1}", screenX, screenY);
prints:
screen pos: X=12; Y=81
camera pos: X=354; Y=108
Upvotes: 1
Reputation: 141
If you use XPath you can target the nodes and retrieve them in to an iterator.
http://msdn.microsoft.com/en-us/library/0ea193ac.aspx
Upvotes: 0