Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Looping Through an XML File with ASP

  • 12-05-2014 10:16am
    #1
    Registered Users, Registered Users 2 Posts: 242 ✭✭


    Having a minor brain fart at the moment (probably as a result of a lack of sleep over the past week!) trying to figure out a way, using ASP, to loop through all the nodes in an XML file and read the values of the nodes with content into variables.

    The XML file is out of my control and updated with new nodes on a fairly regular basis so, rather than subjecting myself to the tedium of editing my code each time a new node is added, I want to try to store the values of all the text nodes in application variables using the full path of the node (e.g. application("parent/child")=xml.selectsinglenode("parent/child").text). This method also has the added benefit of only needing to access the XML file whenever it's updated.

    Anyone got any code to hand they could share?


Comments

  • Registered Users, Registered Users 2 Posts: 403 ✭✭counterpointaud


    Can you give any indication of the structure of the XML file? It sounds like it is relatively flat. You say new nodes are added often, are these always added at the same level as the rest, or can they be nested?

    Is caching the whole file locally an option, and just checking for updates periodically, rather than using application variables?


  • Registered Users, Registered Users 2 Posts: 242 ✭✭MeTV


    There are nested nodes, yes. All the nodes with content that I'm trying to access are 3 to 4 levels deep, including root (see sample below). New nodes can be added at any level with the nodes containing data always being at least 3 levels deep. I also need to allow for the possibility for future additions to be nested deeper than 4 levels.

    Any changes made need to be reflected on the site immediately so caching it on a schedule wouldn't really work.

    SAMPLE STRUCTURE
    <?xml version="1.0" encoding="UTF-8"?>
    <root>
    <group1>
    <child1>data</child1>
    <child2>data</child2>
    <subgroup1>
    <child1>data</child1>
    <child2>data</child2>
    </subgroup1>
    </group1>
    <group2>
    ...
    </group2>
    </root>


  • Registered Users, Registered Users 2 Posts: 403 ✭✭counterpointaud


    OK I am not sure exactly what you need to do. If you only care about getting any text values out, you do something like this to give you a list of element names and values. You may need to get parent or grandparent element names in order to uniquely identify values, in which case you could put key/values in a dictionary instead.
               var names = new List<string>();
                var values = new List<string>();
                var doc = new XmlDocument();
                doc.Load(xmlPath);
                var xmlcontents = doc.InnerXml;
    
                XmlReader rdr = XmlReader.Create(new System.IO.StringReader(xmlcontents));
                while (rdr.Read())
                {
                    if (rdr.NodeType == XmlNodeType.Element)
                    {
                        string name = rdr.LocalName;
                        rdr.MoveToContent();
                        string value = rdr.ReadString();
                        if (!String.IsNullOrWhiteSpace(value))
                        {
                            names.Add(name);
                            values.Add(value);
                        }
                    }            
                }
    
                for(int i = 0; i < names.Count; i++)
                {
                    Console.WriteLine("Element name: {0}", names[i]);
                    Console.WriteLine("Value: {0}", values[i]);
                }
    
                Console.ReadLine();
    
            }
    

    But probably you want to have the XML mapped to some complex objects.


  • Registered Users, Registered Users 2 Posts: 2,793 ✭✭✭John_Mc


    You can use caching and still detect changes to the file. You just need to use a CacheDependency on the file path. The cache will be cleared when the file has been modified.

    I'd +1 counterpointaud's suggestion that you should be serialising/deserialising the xml file to a complex object and writing your code against that object. This is the way it's meant to be done and .NET provides everything you need so you could achieve it in a few lines of code.


  • Registered Users, Registered Users 2 Posts: 242 ✭✭MeTV


    Thanks for the feedback, guys. I've since been informed that the XML files I need to use will now be residing on the same server as my code which takes care of the "overhead" problems I was trying to solve by using application variables. I could probably still save myself some additional "overheads" somewhere along the way but it's now a lower priority than other, more pressing issues in the build.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 2,793 ✭✭✭John_Mc


    MeTV wrote: »
    Thanks for the feedback, guys. I've since been informed that the XML files I need to use will now be residing on the same server as my code which takes care of the "overhead" problems I was trying to solve by using application variables. I could probably still save myself some additional "overheads" somewhere along the way but it's now a lower priority than other, more pressing issues in the build.

    There is still overhead to parsing and reading xml. It's also a lot easier to work with an object in code than an xml document. I'd wager that using the .Net xml serialisation libraries will take less time than the way you're doing it now.

    Best of luck with it


Advertisement