C#XML反序列化错误(2,2) [英] C# XML Deserializing Error (2,2)

查看:723
本文介绍了C#XML反序列化错误(2,2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

学校给了我一个XML文档,而且我必须在屏幕上显示的信息。 。据我所知,XML反序列化将是最容易/最好的解决方案。

School gave me an XML document, and I have to display the information on a screen. As far as I know, Xml Deserialization would be the easiest/nicest solution.

我有这个至今:

public static List<Project> ProjectListDeserialize(string path)
{
    XmlSerializer serializer = new XmlSerializer(typeof(List<Project>));
    Stream filestream = new FileStream(path, FileMode.Open);
    return (List<Project>)serializer.Deserialize(filestream);
}

public static Projects ProjectsDeserialize(string path)
{
    XmlSerializer serializer = new XmlSerializer(typeof(Projects));
    Stream filestream = new FileStream(path, FileMode.Open);
    return (Projects)serializer.Deserialize(filestream);
}



这是XML文档的样子:

And this is how the XML document looks like:

<?xml version="1.0" encoding="utf-16" ?>    
<Projects xmlns="http://www.pulse.nl/DynamicsAX/2009/DataSets" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Project ID ="1000.0001" CustomerID="1000">
        <Name>Project data key performance indicators</Name>
        <Status>WorkInProgress</Status>
        <StartDate>2011-01-01</StartDate>
        <ExpectedCompletionDate>2011-08-01</ExpectedCompletionDate>
        <CompletionDate xsi:nil="true" />
    </Project>
    <Project ID ="1000.0008" CustomerID="1000" ParentID="1000.0001">
        <Name>Implementation</Name>
        <Status>WaitListed</Status>
        <StartDate>2011-06-01</StartDate>
        <ExpectedCompletionDate>2011-08-01</ExpectedCompletionDate>
        <CompletionDate xsi:nil="true" />
    </Project>
</Projects>



这两种方法都抛出同样的异常:

Both methods are throwing the same exception:

<Projects xmlns='http://www.pulse.nl/DynamicsAX/2009/DataSets was not expected

我怎样才能正确地反序列化上面的XML?任何样品将是有益的!

How can I correctly deserialize the above xml? Any samples would be helpful!

推荐答案

尝试指定的默认命名空间中的 =http://msdn.microsoft.com/en-us/library/swxzdhc0.aspx 相对=nofollow> XmlSerializer的

Try specifying the default namespace for the XML document in the constructor of the XmlSerializer:

var serializer = new XmlSerializer(typeof(Projects), "http://www.pulse.nl/DynamicsAX/2009/DataSets");

相关资源:

  • XmlSerializer Constructor
  • XML namespace

这篇关于C#XML反序列化错误(2,2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆