C# XML 反序列化错误 (2,2)

发布于 2024-11-02 04:37:54 字数 1799 浏览 0 评论 0 原文

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

到目前为止我已经有了:

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 文档的样子:

<?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>

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

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

如何正确反序列化上述 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.

I have this so far:

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);
}

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凌乱心跳 2024-11-09 04:37:54

最有可能的问题是您没有指定正确的命名空间作为 Project 类的属性。

您可以告诉 XmlSerializer 在反序列化期间忽略命名空间(检查 此回答)。

或者,您可以使用 XmlTypeAttribute 设置适当的命名空间:

[XmlType(Namespace = "http://www.pulse.nl/DynamicsAX/2009/DataSets")]
public class Project 
{
   ...
}

Most likely the problem is that you didn't specify the right namespace as an attribute for your Project class.

You can tell the XmlSerializer to ignore the namespaces during deserialization (check this answer).

Alternatively, you can set the appropriate namespace using the XmlTypeAttribute:

[XmlType(Namespace = "http://www.pulse.nl/DynamicsAX/2009/DataSets")]
public class Project 
{
   ...
}
━╋う一瞬間旳綻放 2024-11-09 04:37:54

尝试在 默认命名空间 http://msdn.microsoft.com/en-us/library/swxzdhc0.aspx" rel="nofollow">XmlSerializer:

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

相关资源:

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");

Related resources:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文