Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

XML parsing problem

Discussion in 'Scripting' started by DigitalToy, May 13, 2013.

  1. DigitalToy

    DigitalToy

    Joined:
    Oct 29, 2012
    Posts:
    11
    I am parsing an XML file using code similar to this (I removed things to make it shorter)

    XmlDocument doc = new XmlDocument();

    doc.LoadXml(fileText);

    XmlNodeList fileLabels = doc.GetElementsByTagName("label");

    label.text = fileLabels[0].InnerXml;

    It works fine, except for its treatment of XML predefined entities. In particular, it parses " and ' correctly, returning " and ', but it does not parse & and > returning & and > instead of and >.

    Does anyone know what could cause this?
     
  2. DigitalToy

    DigitalToy

    Joined:
    Oct 29, 2012
    Posts:
    11
    Does anyone have any ideas?
     
  3. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,466
    Not really a Unity-specific question, but typically if you have a lot of special characters that you need to parse you should wrap the XmlText in CDATA and access the value with the Value property instead (which might work in your case without the CDATA). Alternately - just serialize your data into a class with a bunch of fields instead of traversing the DOM manually.
     
  4. dkozar

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410
    That's right. I would also point you towards the XML serialization, instead of doing it manually using the XmlDocument.

    Just look for the XML Serialization throughout the forum, or Google it.

    It's a consistent and buletproof way of doing things.