Reading an XML From C# menu

Shout-Out

User Tag List

Results 1 to 6 of 6
  1. #1
    DarkLinux's Avatar Former Staff
    CoreCoins Purchaser Authenticator enabled
    Reputation
    1627
    Join Date
    May 2010
    Posts
    1,846
    Thanks G/R
    193/539
    Trade Feedback
    16 (100%)
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)

    Reading an XML From C#

    Hello,
    I am trying to read from a XML file in C#. I have coded a waypoints system. I can save the waypoints in an XML file. My problem is I have little free time to work on it. I ran into a problem trying to read from my XML. Any help like examples to helpful links will suffice.

    XML File
    Code:
    <?xml version="1.0"?>
    <Path>
          <Name>SW Test</Name>
          <Nodes>
                <Node id="0" x="-8832.103" y="637.4868" z="0" />
                <Node id="1" x="-8832.148" y="637.5655" z="0" />
                <Node id="2" x="-8827.26" y="637.9915" z="0" />
                <Node id="3" x="-8822.965" y="637.6017" z="0" />
                <Node id="4" x="-8818.634" y="640.0749" z="0" />
                <Node id="5" x="-8814.84" y="639.9224" z="0" />
                <Node id="6" x="-8811.952" y="636.7586" z="0" />
                <Node id="7" x="-8811.789" y="631.8894" z="0" />
                <Node id="8" x="-8812.003" y="631.8409" z="0" />
                <Node id="9" x="-8818.141" y="628.4603" z="0" />
                <Node id="10" x="-8820.524" y="627.1432" z="0" />
          </Nodes>
          <Sellers>
                <Seller id="0" />
          </Sellers>
    </Path>
    This is the code I have come up with, it does not work.
    Code:
               //* create an xml document object.
                XmlDocument xmlDoc = new XmlDocument(); 
                
               //* load the XML document from the specified file,
                xmlDoc.Load(@"C:\\xml2.xml"); 
                
    
                //* Get elements.
                XmlNodeList node = xmlDoc.GetElementsByTagName("Nodes");
                XmlNodeList seller = xmlDoc.GetElementsByTagName("Sellers");
    
                //* Display the results.
                for (int i = 0; i < 1000; i++)
                {
                    OutPut("Node ID: " + node[i].InnerText);
                }
                OutPut("Seller ID: " + seller[0].InnerText);
    I know it has something to do with the fact that node has 3 vars and I am only reading one. I just can figure it out.

    Thanks for your help and time. +Rep to all that help!
    Last edited by DarkLinux; 08-02-2010 at 11:08 PM.

    Reading an XML From C#
  2. #2
    suicidity's Avatar Contributor
    Reputation
    207
    Join Date
    Oct 2006
    Posts
    1,439
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Some dummy code..

    Code:
    public static List<Node> LoadFile(string filename)
    {
    	if (File.Exists(filename) == false)
    		return false;
    
    	XmlReader Reader = XmlReader.Create(filename);
    	List<Node> NodeList = new List<Node>();
    	while (Reader.Read())
    	{
    		if (Reader.NodeType != XmlNodeType.Element)
    			continue;
    
    		Node ParsedNode = new Node();
    		int.TryParse(Reader.GetAttribute("id"), out ParsedNode.Id);
    		float.TryParse(Reader.GetAttribute("x"), out ParsedNode.X);
    		float.TryParse(Reader.GetAttribute("y"), out ParsedNode.Y);
    		float.TryParse(Reader.GetAttribute("z"), out ParsedNode.Z);
    
    		NodeList.Add(ParsedNode);
    	}
    
    	Reader.Close();
    	return NodeList;
    }


  3. #3
    mager1794's Avatar Member
    Reputation
    356
    Join Date
    Feb 2008
    Posts
    703
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would suggest using xml serialization. Since its based on your own format and everything - itd be way easier
    Lunar Gaming - Reaching For The Stars

  4. #4
    Apoc's Avatar Angry Penguin
    Reputation
    1388
    Join Date
    Jan 2008
    Posts
    2,750
    Thanks G/R
    0/13
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If using .NET 3 or newer, you can use LINQ.

    Code:
    (from e in XElement.Load(filename).Descendants("Node")
                             let x = float.Parse(e.Attribute("x").Value)
                             let y = float.Parse(e.Attribute("y").Value)
                             let z = float.Parse(e.Attribute("z").Value)
                             select new Node(x, y, z)).ToList();


    Obviously you'll want to fix the query, but that's just an example.

  5. #5
    DarkLinux's Avatar Former Staff
    CoreCoins Purchaser Authenticator enabled
    Reputation
    1627
    Join Date
    May 2010
    Posts
    1,846
    Thanks G/R
    193/539
    Trade Feedback
    16 (100%)
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Thanks so much for the help! You have no idea how much this has helped.

    Thanks to Apoc, suicidity and mager1794. +Rep

  6. #6
    Hex00010's Avatar Master Sergeant
    Reputation
    45
    Join Date
    Feb 2010
    Posts
    98
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just to throw it out there here is our XML reader code for our APB Emulator

    using System; using System.Co - Anonymous - ZsWiWmxG - Pastebin.com

Similar Threads

  1. Read Binary Buffer from Wow.exe into memory C++
    By baintzimisce in forum WoW Memory Editing
    Replies: 4
    Last Post: 10-24-2009, 03:20 PM
  2. Reading multiple strings from another file?
    By Ryoushi. in forum Programming
    Replies: 0
    Last Post: 06-08-2009, 12:08 PM
  3. Replies: 9
    Last Post: 04-27-2009, 10:55 AM
  4. [Guide][VB.NET] Read a string from memory
    By Gothian in forum Programming
    Replies: 14
    Last Post: 08-18-2008, 04:39 PM
  5. [Guide][VB.NET] Reading a String From Memory
    By Gothian in forum WoW Memory Editing
    Replies: 14
    Last Post: 01-18-2008, 12:08 PM
All times are GMT -5. The time now is 07:35 AM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search