Selecting an XML node

Started by
4 comments, last by Alpha_ProgDes 15 years, 7 months ago
I'm not quite sure on the syntax.
<root>
    <blah_1 />
    <blah_2 />
    <blah_3 />
    <I_foo_yuo />
</root>
I want to get all nodes that start with "blah". Now it is:

Dim assemblerList As XmlNodeList = xmlformdocument.SelectNodes("/root/*[starts-with(? , 'blah')]")
I don't know what to put in the ? place. I've googled but haven't come up with anything.

Beginner in Game Development?  Read here. And read here.

 

Advertisement
I believe I found it:
Dim assemblerList As XmlNodeList = xmlformdocument.SelectNodes("/root/*[starts-with(name() , 'blah')]")


Am I correct? Is there a better way? More standards way?

Beginner in Game Development?  Read here. And read here.

 

Looks sane to me.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
If the XML schema is under your control, consider that the fact that you want to be able to do this suggests a design problem.

Instead:

<root>    <blah type='1' />    <blah type='2' />    <blah type='3' />    <I_foo_yuo /></root>
Quote:Original post by Zahlman
If the XML schema is under your control, consider that the fact that you want to be able to do this suggests a design problem.

Instead:

*** Source Snippet Removed ***


Alternatively if its under your control you can not have to deal with xml at all and use the xml serializer, I know it looks complex but the advantage is you can serialize a entire tree of objects all at once automatically.
using System;using System.Collections.Generic;using System.Xml;using System.Xml.Serialization;using System.IO;namespace XmlTest{    public class Blah    {        public Blah()        {}        public Blah(int type)        {            Type = type;        }        [XmlAttribute("type")]        public int Type { get; set; }    }    [XmlRoot("root")]    public class XmlExample    {                private List<Blah> list = new List<Blah>();        [XmlElement("blah")]        public Blah[] ListItems        {            get { return list.ToArray(); }            set            {                list = new List<Blah>();                list.AddRange(value);            }        }        public void Add(Blah blah)        {            list.Add(blah);        }            }    class XmlManager {        public void Save(string path,XmlExample obj)        {            XmlSerializer s = new XmlSerializer(obj.GetType());            TextWriter w = new StreamWriter(path);            s.Serialize(w, obj);            w.Close();        }        public XmlExample Load(string path)        {            XmlExample obj = new XmlExample();            XmlSerializer s = new XmlSerializer(obj.GetType());            TextReader r = new StreamReader(path);            obj = (XmlExample)s.Deserialize(r);            r.Close();            return obj;        }    }}


use

XmlExample test = new XmlExample();test.Add(new Blah(1));test.Add(new Blah(2));test.Add(new Blah(3));XmlManager xman = new XmlManager();xman.Save("..\\..\\xml\\test.xml", test);


for output
<?xml version="1.0" encoding="utf-8"?><root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">  <blah type="1" />  <blah type="2" />  <blah type="3" /></root>
Quote:Original post by Zahlman
If the XML schema is under your control, consider that the fact that you want to be able to do this suggests a design problem.

Instead:

*** Source Snippet Removed ***


Schema's not under my control. But thank you [smile]

Beginner in Game Development?  Read here. And read here.

 

This topic is closed to new replies.

Advertisement