0

I have an external xml feed that is pretty poorly structured so Im not sure how to deserialize it directly.

I can not use System.IO.Linq in my assembly so the only solution I know of cant be used.

Example of this xml is

<body>
    <route>
        <stop tag="Info I need to get"/>
        <stop tag="Info I need to get"/>
        <path>
            <point tag="info I need to get"/>
            <point tag="info I need to get"/>
            <point tag="info I need to get"/>
        </path>
        <path>
            <point tag="info I need to get"/>
        </path>
        <path>
            <point tag="info I need to get"/>
            <point tag="info I need to get"/>
        </path>
    </route>
</body>

if I could somehow parse all the path points into an array I can get the data inside the tags easily.

the linq solution I was refering to is mentioned at How to parse multiple single xml elements in .Net C#

5
  • You could simply read the XML and parse it the old fashion way without using any Serialization process. Commented Nov 16, 2012 at 17:07
  • Are you talking about through a series of string expressions? Id rather leave that as the last resort if possible lol. Commented Nov 16, 2012 at 17:09
  • What? No... I meant getting a XMLDocument stream and navigating the nodes manually. Commented Nov 16, 2012 at 17:11
  • ...you said old fashioned haha Commented Nov 16, 2012 at 17:24
  • Alright... Regular expression your ass out of it. :p Commented Nov 16, 2012 at 17:27

5 Answers 5

3

You can use XPath to select all nodes having tag attribute

XmlDocument doc = new XmlDocument();
doc.Load("myfile.xml");

foreach (XmlElement node in doc.SelectNodes("//*[@tag]"))
{
    Console.WriteLine(node.Name + ": " + node.GetAttribute("tag"));
}
Sign up to request clarification or add additional context in comments.

Comments

2

I would recomend using array list since I would guess you do not now exact number of nodes

ArrayList list = new ArrayList();
XmlDocument doc = new XmlDocument();
doc.Load("sample.xml");
XmlElement root = doc.DocumentElement;
XmlNodeList nodes = root.SelectNodes("point"); // You can also use XPath here
foreach (XmlNode node in nodes)
{
   list.Add(node);
}

Comments

2

Can you use XmlDocument?

You might need to learn some XPath to be able to navigate it well, but this should do the trick.

You could also just do something simple like this:

XmlDocument doc = new XmlDocument();
doc.Load("myfile.xml");
foreach(XmlNode node in doc.SelectNodes("point"))
{
    var valueYouWant = node.Attributes["tag"].Value;
    // etc.
} 

Comments

0

You could use an XmlSerializer :

Here is a good example: Simple XmlSerializer example

This would allow you to serialize the object for storage.

Comments

0

Another way would be to use the xml reader XML Reader MSDN you can just look for elements of type path and load the attribute data using something similar to Reading Attributes

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.