Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 反序列化字符串C时出现的问题#_Xml_String_Serialization - Fatal编程技术网

Xml 反序列化字符串C时出现的问题#

Xml 反序列化字符串C时出现的问题#,xml,string,serialization,Xml,String,Serialization,我从php脚本的输出中提取xml数据。它的工作方式是php脚本将xml数据输出到html页面,API im使用将该页面上显示的所有文本数据拉入字符串 所以html/php的输出应该是非常标准的xml格式,我研究了它并修改了php,以确保即使是最小的细节也能正确输出 然后,我使用以下代码将字符串反序列化到我的类中 public static object XmlDeserializeFromFile(Type type, string strPath) { object data;

我从php脚本的输出中提取xml数据。它的工作方式是php脚本将xml数据输出到html页面,API im使用将该页面上显示的所有文本数据拉入字符串

所以html/php的输出应该是非常标准的xml格式,我研究了它并修改了php,以确保即使是最小的细节也能正确输出

然后,我使用以下代码将字符串反序列化到我的类中

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;
}
所以问题是,从我的代码中解析字符串时没有遇到异常,这使我认为它在某些方面起作用。但是,该类仍然返回空值。我还验证了字符串是否被正确收集,因为它在我的调试器中显示了完整的xml内容

使用以下代码测试类Im

    MyClass member = (MyClass)XmlDeserializeFromFile(typeof(MyClass), MyString);
    if(member != null)
    {
        Debug.Log(member.a);
    }
    else
    {
        Debug.Log("Not Loaded");
    }
调试器中的结果一直显示member.a=null


我的类属性都是使用{get;set;}公开的,并且与xml完全匹配,如果从文件而不是字符串加载,则类值将正确填充。

查看xmlSerializer中可用的事件:UnknownAttribute、UnknownElement、UnknownNode、UnreferencedObject。设置EventsHandler以在事件发生时处理这些事件。这些事件产生的数据应该会引导您找到正确的方向,说明为什么数据没有显示在您的对象中。

问题最终变得极其愚蠢,但您的建议几乎立刻帮我解决了。谢谢