0

Im pulling in xml data from the output of a php script. The way it works is that the php script outputs the xml data to the html page and the API im using pulls all text data that is displayed on that page into a string.

so the output of the html/php is supposed to be pretty standard xml format, Ive studied it and altered the php to make sure even the smallest details are outputted correctly.

Im then using the following code to deserialize the string into my class.

public static object XmlDeserializeFromFile(Type type, string strPath)
{
    object data;

    try
    {
        using (StreamReader reader = new StreamReader(strPath))
        {
            XmlSerializer serializer = new XmlSerializer(type);
            data = serializer.Deserialize(reader);
            reader.Close();
        }
    }
    catch(Exception exception)
    {
        Debug.LogError (exception);
        data = type.GetConstructor(Type.EmptyTypes).Invoke(null);
    }

    return data;
}

So the issue is that the string gets parsed from my code without hitting an exception, which makes me think that its working on some regard. However, the class still returns null values. Ive also verified that the string is being collected properly because it is displaying the full xml content in my debugger.

To test the class Im using the following code.

    MyClass member = (MyClass)XmlDeserializeFromFile(typeof(MyClass), MyString);
    if(member != null)
    {
        Debug.Log(member.a);
    }
    else
    {
        Debug.Log("Not Loaded");
    }

And the result in my debugger keeps showing member.a = null.

My class properties are all public with {get; set;} and match the xml exactly, the class values populate correctly if i load from a file instead of a string.

1 Answer 1

1

Take a look at the events that are available in xmlSerializer: UnknownAttribute, UnknownElement, UnknownNode,UnreferencedObject. Set up eventshandlers to handle those events if/when they occur. The data that comes out of those events should lead you in the right direction on why data isn't showing up in your object.

Sign up to request clarification or add additional context in comments.

1 Comment

The problem ended up being something extremely stupid, but your advice helped me figure it out almost immediately. Thanks!

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.