tags:

views:

61

answers:

4

I have this code:

public class Hero
{
    XmlReader Reader = new XmlTextReader("InformationRepositories/HeroRepository/HeroInformation.xml");
    XmlReaderSettings XMLSettings = new XmlReaderSettings();        

    public ImageSource GetHeroIcon(string Name)
    {
        XMLSettings.IgnoreWhitespace = true;
        XMLSettings.IgnoreComments = true;
        Reader.MoveToAttribute(" //I'm pretty much stuck here.
    }

}

And this is the XML file I want to read from:

<?xml version="1.0" encoding="utf-8" ?>
<Hero>
  <Legion>
    <Andromeda>
      <HeroType>Agility</HeroType>
      <Damage>39-53</Damage>
      <Armor>3.1</Armor>
      <MoveSpeed>295</MoveSpeed>
      <AttackType>Ranged(400)</AttackType>
      <AttackRate>.75</AttackRate>
      <Strength>16</Strength>
      <Agility>27</Agility>
      <Intelligence>15</Intelligence>
      <Icon>Images/Hero/Andromeda.gif</Icon>      
    </Andromeda>    
  </Legion>

  <Hellbourne>

  </Hellbourne>
</Hero>

I'm tring to get the ,/Icon> element.

+1  A: 

MoveToAttribute() won't help you, because everything in your XML is elements. The Icon element is a subelement of the Andromeda element.

One of the easiest ways of navigating an XML document if you're using the pre-3.5 xml handling is by using an XPathNavigator. See this example for getting started, but basically you just need to create it and call MoveToChild() or MoveToFollowing() and it'll get you to where you want to be in the document.

XmlDocument doc = new XmlDocument();
doc.Load("InformationRepositories/HeroRepository/HeroInformation.xml");

XPathNavigator nav = doc.CreateNavigator();

if (nav.MoveToFollowing("Icon",""))
    Response.Write(nav.ValueAsInt);

Note that an XPathNavigator is a forward only mechanism, so it can be problematic if you need to do looping or seeking through the document.

If you're just reading XML to put the values into objects, you should seriously consider doing this automatically via object serialization to XML. This would give you a painless and automatic way to load your xml files back into objects.

womp
A: 

Mark your attributes in your object according to the element you want to load them to: See: http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlattributeattribute.aspx

If, for some reason, you can't do this to your current object, consider making a bridge object which mirrors your original object and add a AsOriginal() method which returns the Original Object.

Working off the msdn example:

public class GroupBridge
{
   [XmlAttribute (Namespace = "http://www.cpandl.com")]
   public string GroupName;

   [XmlAttribute(DataType = "base64Binary")]
   public Byte [] GroupNumber;

   [XmlAttribute(DataType = "date", AttributeName = "CreationDate")]
   public DateTime Today;

   public Group AsOriginal()
   {
    Group g = new Group();
    g.GroupName = this.GroupName;
    g.GroupNumber = this.GroupNumber;
    g.Today = this.Today;

    return g;
   }
}

public class Group
{
   public string GroupName;
   public Byte [] GroupNumber;
   public DateTime Today;
}

To Serialize and DeSerialize from LINQ objects, you can use:

    public static string SerializeLINQtoXML<T>(T linqObject)
    {
         // see http://msdn.microsoft.com/en-us/library/bb546184.aspx
         DataContractSerializer dcs = new DataContractSerializer(linqObject.GetType());

         StringBuilder sb = new StringBuilder();
         XmlWriter writer = XmlWriter.Create(sb);
         dcs.WriteObject(writer, linqObject);
         writer.Close();

         return sb.ToString();
     }

     public static T DeserializeLINQfromXML<T>(string input)
     {
         DataContractSerializer dcs = new DataContractSerializer(typeof(T));

         TextReader treader = new StringReader(input);
         XmlReader reader = XmlReader.Create(treader);
         T linqObject = (T)dcs.ReadObject(reader, true);
         reader.Close();

         return linqObject;
     }

I don't have any example code of Serialization from non-LINQ objects, but the MSDN link should point you in the right direction.

Jim Schubert
A: 

you can use this regular exspression "<Icon>.*</Icon>" to find all the icons then just remove the remove the tag, and use it....

would be a lot shorter

  Regex rgx = new Regex("<Icon>.*</Icon>");
   MatchCollection matches = rgx.Matches(xml);

   foreach (Match match in matches)
    {
    string s= match.Value;
    s= s.Remove(0,6)
    s= s.Remove(s.LastIndexOf("</Icon>"),7);
    console.Writeline(s);
    }
Hellfrost
A: 

You can use linq to xml:


    public class XmlTest
    {
        private XDocument _doc;

        public XmlTest(string xml)
     {
      _doc = XDocument.Load(new StringReader(xml);
     }

        public string Icon { get { return GetValue("Icon"); } }

        private string GetValue(string elementName)
        {
            return _doc.Descendants(elementName).FirstOrDefault().Value;
        }
    }
Jens Granlund