Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9318553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:09:26+00:00 2026-06-19T03:09:26+00:00

I have some XML that looks like this (highly simplified): <?xml version=1.0?> <example> <shortcuts>

  • 0

I have some XML that looks like this (highly simplified):

<?xml version="1.0"?>
<example>
    <shortcuts>
        <shortcut name="shortcut1">
            <property name="name1" value="value1" />
            <property name="name2" value="value2" />
        </shortcut>
    </shortcuts>
    <data>
        <datum name="datum1">
            <property name="name1" value="value1" />
            <property name="name2" value="value2" />    
        </datum>
        <datum name="datum2">
            <shortcutRef name="shortcut1" />
        </datum>
        <datum name="datum3">
            <shortcutRef name="shortcut1" />
            <property name="name3" value="value3" />    
        </datum>
    </data>
</example>

As you can see, it is structured such that “shortcuts” can be defined which consist of one or more properties. Data can then be described explicitly with properties, or one or more shortcuts, or a mix of both (and there is no specific order).

I want to parse this with XmlReader (XmlDocument would be easier but won’t work here because the XML file is too large). I thought a good way to do this would be to store XML subtrees of each shortcut in a dictionary keyed by the shortcut names, which are unique. Then when they are referenced, I could just read through that subtree XmlReader rather than the main one. However the subtree XmlReader must still be linked to the main XmlReader because the XML that comes out is not what I expect. Here is my code:

using(XmlReader xml = XmlReader.Create("example.xml"))
{
    Dictionary<string, XmlReader> shortcuts = new Dictionary<string, XmlReader>();
    xml.ReadToDescendant("shortcuts");
    xml.ReadToDescendant("shortcut");
    do
    {
        shortcuts.Add(xml.GetAttribute("name"), xml.ReadSubtree());
    } while(xml.ReadToNextSibling("shortcut"));

    xml.ReadToFollowing("data");
    while(xml.ReadToFollowing("datum"))
    {
        Console.WriteLine(xml.GetAttribute("name"));

        XmlReader datum = xml.ReadSubtree();
        while(datum.Read())
        {
            if(datum.Name == "property")
            {
                Console.WriteLine(datum.GetAttribute("name") + ':' + datum.GetAttribute("value"));
            }
            else if(datum.Name == "shortcutRef")
            {
                XmlReader shortcut_ref = shortcuts[datum.GetAttribute("name")];
                while(shortcut_ref.ReadToFollowing("property"))
                {
                    Console.WriteLine(shortcut_ref.GetAttribute("name") + ':' + shortcut_ref.GetAttribute("value"));
                }
            }
        }
    }
}

What is the best way to parse XML that is structured in this way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T03:09:27+00:00Added an answer on June 19, 2026 at 3:09 am

    It’s not entirely clear what you want to do – but since you use the words “play back” then I am guessing you don’t need to store ALL the values from the XML nodes (data / datum) in memory (you can discard them after use), however you need to cache the shortcut properties so that you can re-iterate through them when they are referenced… You just about had it, but instead of storing the XML nodes, just store the objects instead in the dictionary.

    public class Property
    {
        public string Name { get; set; }
        public string Value { get; set; }
    }
    
    public class Shortcut
    {
        public List<Property> Properties = new List<Property>();
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            FileStream fs = new FileStream(@"c:\temp\example.xml", FileMode.Open, FileAccess.Read);
            XmlTextReader reader = new XmlTextReader(fs);
    
            Dictionary<string, Shortcut> ShortcutDictionary = new Dictionary<string, Shortcut>();
    
            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element && reader.LocalName == "shortcuts")
                {
                    while (reader.Read())
                    {
                        if (reader.NodeType == XmlNodeType.Element && reader.LocalName == "shortcut")
                        {
                            Shortcut shortcut = new Shortcut();
                            ShortcutDictionary.Add(reader.GetAttribute("name"), shortcut);
                            while (reader.Read())
                            {
                                if (reader.NodeType == XmlNodeType.Element && reader.LocalName == "property")
                                    shortcut.Properties.Add(new Property() { Name = reader.GetAttribute("name"), Value = reader.GetAttribute("value") });
                                else if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName == "shortcut")
                                    break;
                            }
                        }
                        else if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName == "shortcuts")
                            break;
                    }
                }
    
                if (reader.NodeType == XmlNodeType.Element && reader.LocalName == "data")
                {
                    while (reader.Read())
                    {
                        if (reader.NodeType == XmlNodeType.Element && reader.LocalName == "datum")
                        {
                            while (reader.Read())
                            {
                                if (reader.NodeType == XmlNodeType.Element && reader.LocalName == "property")
                                {
                                    Console.WriteLine(reader.GetAttribute("name") + ':' + reader.GetAttribute("value"));
                                }
                                else if (reader.NodeType == XmlNodeType.Element && reader.LocalName == "shortcutRef")
                                {
                                    foreach (Property property in ShortcutDictionary[reader.GetAttribute("name")].Properties)
                                        Console.WriteLine(property.Name + ':' + property.Value);
                                }
                                else if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName == "datum")
                                    break;
                            }
                        }
                        else if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName == "data")
                            break;
                    }
                }
            }
    
            reader.Close();
            fs.Close();
        }
    }
    

    Otherwise, if that is not it, then you are trying to access serial data in a random access manner. Your best bet would be to convert/save the data into a database. Something like SQLite would do it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data in an XML element that looks like this: <?xml version=1.0
I have some xml that looks like this: <?xml version=1.0?> <data> <items> <item><timestamp>2011-07-11T09:01:42Z</timestamp><title><![CDATA[ some
I have some xml that looks like this: <xml><name>oscar</name><race>puppet</race><class>grouch</class></xml> The tags change and are
Okay, so I've got some example xml that looks like this: <Node name=details> <Node
I have some XML code that looks like this <SEARCHRESULTS> <FUNCTION name=BarGraph> <PARAMETER name=numList></PARAMETER>
I have some xml that looks like this... <tbody> <tr> <td> <h5> <a class=foo
I have an XML file that looks like this: <?xml version=1.0 standalone=yes?> <NewDataSet> <DT100>
If I have an XML file that looks like this: <properties> <property> <picture>http://example.com/image1.jpg</picture> <picture>http://example.com/image2.jpg</picture>
I have some XML that looks like <?xml version=1.0?> <root> <![CDATA[ > foo ]]>
I have some xml that looks like this: <rootElement attribute=' > '/> This is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.