在Silverlight中反序列化XML而不使用声明或命名空间

时间:2013-04-09 15:59:14

标签: c# xml silverlight

我已经获得了XML数据源,我需要将其反序列化为Silverlight(v5)应用程序中的对象。数据如下:

<AgentState>
  <agentName>jbloggs</agentName>
  <extension>12345</extension>
  <currentlyIn>TestStatus</currentlyIn>
</AgentState>

我已经在Silverlight端创建了一个类,我试图将这个XML - 你注意到,它缺少一个声明和命名空间 - 转换为对象。

StringReader sr = null;
string data = Encoding.UTF8.GetString(e.Buffer, e.Offset, e.BytesTransferred);
sr = new StringReader(data);
XmlSerializer xs = new XmlSerializer(typeof (AgentState));
AgentState agent = (AgentState) xs.Deserialize(sr);

..但是这会引发错误an error in xml document (1,2),因为它错过了声明。即使手动添加虚拟声明也会导致有关缺少名称空间的进一步错误。

我发现other有关忽略XML中的命名空间/声明的问题,但这些似乎都不适用于Silverlight。

有人可以建议将这种XML反序列化为对象的最佳方法吗?

3 个答案:

答案 0 :(得分:2)

这似乎有效:

    public class AgentState
    {
        public string agentName { get; set; }
        public string extension { get; set; }
        public string currentlyIn { get; set; }
    }

    static void Main(string[] args)
    {
        var s = @"<AgentState>
                    <agentName>jbloggs</agentName>
                    <extension>12345</extension>
                    <currentlyIn>TestStatus</currentlyIn>
                </AgentState>";

        XmlSerializer serializer = new XmlSerializer(typeof(AgentState));
        var ms = new MemoryStream(Encoding.UTF8.GetBytes(s));
        var obj = serializer.Deserialize(ms);
    }

答案 1 :(得分:1)

我想知道将xml声明附加到字符串有什么问题。这似乎工作正常:

[System.Xml.Serialization.XmlRootAttribute("AgentState")]
public class AgentState
{
    public string agentName {get; set;}
    public int extension {get; set;}
    public string currentlyIn {get; set;}
}

public void RunSerializer()
{
    System.Xml.Serialization.XmlSerializer agent_serializer =
       new System.Xml.Serialization.XmlSerializer(typeof(AgentState));

    string agent_state_text = File.ReadAllText(@"C:\Temp\AgentState.xml");
    Console.WriteLine(agent_state_text + Environment.NewLine);
    string xml_agent_state = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + agent_state_text;
    Console.WriteLine(xml_agent_state + Environment.NewLine);

    AgentState agent_state = new AgentState();
    using(StringReader tx_reader = new StringReader(xml_agent_state))
    {
        if (tx_reader != null)
        {
            agent_state = (AgentState)agent_serializer.Deserialize(tx_reader);
        }
    }
    Console.WriteLine(agent_state.agentName);
    Console.WriteLine(agent_state.extension);
    Console.WriteLine(agent_state.currentlyIn);
}

输出:

<AgentState>
  <agentName>jbloggs</agentName>
  <extension>12345</extension>
  <currentlyIn>TestStatus</currentlyIn>
</AgentState>

<?xml version="1.0" encoding="UTF-8"?>
<AgentState>
  <agentName>jbloggs</agentName>
  <extension>12345</extension>
  <currentlyIn>TestStatus</currentlyIn>
</AgentState>

jbloggs
12345
TestStatus

答案 2 :(得分:0)

我已经设法使用以下代码使其工作 - 我不相信它是“正确”的做事方式,但似乎有效:

 string data = Encoding.UTF8.GetString(e.Buffer, e.Offset, e.BytesTransferred);

 var document = XDocument.Parse(data);
 AgentState agent= (from c in document.Elements()
                        select new AgentState()
                                   {
                                       agentName = c.Element("agentName").Value,
                                       extension = c.Element("extension").Value,
                                       currentlyIn=c.Element("currentlyIn").Value
                                   }).Single();

感谢您的建议,它让我走上正轨。