使用 C# 在 Windows CE 中加载 XML 文档

发布于 2024-12-04 16:16:30 字数 2258 浏览 0 评论 0原文

我正在尝试使用 C# 在 Windows CE 中加载 XML 文档,但是在调试过程中我不断遇到一些奇怪的错误。这是我的代码:

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace cpap3
{
public class Program
{
    public static void Display()
    {

        XmlDocument docXML = new XmlDocument();

        docXML.Load(Environment.GetFolderPath(Environment.SpecialFolder.Programs)+"\\modulos.xml");


        XmlElement nodRoot = docXML.DocumentElement;

        XmlNodeList nodItems = nodRoot.GetElementsByTagName("Modulos");

        for (int i = 0; i < nodItems.Count; i++)
            Console.WriteLine(nodItems[i].InnerXml);

        for (int i = 0; i < nodItems.Count; i++)
            Console.WriteLine(nodItems[i].InnerXml);

    }

    static int Main(string[] args)
    {

        Display();           

        return 0;
    }
}
}

我做错了什么吗? XML 文件位于正确的位置,并且我已经尝试了该文件的多个位置。这是我收到的错误。

{“无法显示错误消息,因为找不到包含它的可选资源程序集 无法显示错误消息,因为找不到包含它的可选资源程序集”}

加载 XML 文件时发生错误:

docXML.Load( Environment.GetFolderPath(Environment.SpecialFolder.Programs)+"\modulos.xml");

这是异常详细信息:

System.Xml.XmlException was unhandled
  Message="An error message cannot be displayed because an optional resource assembly   containing it cannot be found An error message cannot be displayed because an optional resource assembly containing it cannot be found"
  LineNumber=1
  LinePosition=31
  SourceUri="file:///Windows/Programs/modulos.xml"
  StackTrace:
at System.Xml.XmlTextReaderImpl.Throw()
at System.Xml.XmlTextReaderImpl.Throw()
at System.Xml.XmlTextReaderImpl.CheckEncoding()
at System.Xml.XmlTextReaderImpl.ParseXmlDeclaration()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlLoader.Load()
at System.Xml.XmlDocument.Load()
at System.Xml.XmlDocument.Load()
at ConsoleApplication3.Program.Display()
at ConsoleApplication3.Program.Main()

---------------编辑--------------

我花了一段时间,但现在我有了真正的错误报告:) ...这是实际的错误。

"System does not support 'iso-8859-1' encoding. Line 1, position 31." 

我尝试使用不同的编码保存文件,但我不断收到相同的错误。有什么解决办法吗?

- - - - - - - -编辑 - - - - - - - - - 我已经解决了:D。我正在使用记事本更改编码,但我忘记了我在 XML 文件中写入了编码标记,这导致了此问题。愚蠢的错误。 :(

I´m trying to load a XML document in Windows CE using C#, however i keep getting some strange errors in debugging. Here is my code:

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace cpap3
{
public class Program
{
    public static void Display()
    {

        XmlDocument docXML = new XmlDocument();

        docXML.Load(Environment.GetFolderPath(Environment.SpecialFolder.Programs)+"\\modulos.xml");


        XmlElement nodRoot = docXML.DocumentElement;

        XmlNodeList nodItems = nodRoot.GetElementsByTagName("Modulos");

        for (int i = 0; i < nodItems.Count; i++)
            Console.WriteLine(nodItems[i].InnerXml);

        for (int i = 0; i < nodItems.Count; i++)
            Console.WriteLine(nodItems[i].InnerXml);

    }

    static int Main(string[] args)
    {

        Display();           

        return 0;
    }
}
}

Am I doing something wrong? The XML file is in the right place, and I've tried several locations for the file. This is the error I'm getting.

{"An error message cannot be displayed because an optional resource assembly containing it cannot be found An error message cannot be displayed because an optional resource assembly containing it cannot be found"}

The error occurs when I load the XML file:

docXML.Load(Environment.GetFolderPath(Environment.SpecialFolder.Programs)+"\modulos.xml");

Here is the exception detail:

System.Xml.XmlException was unhandled
  Message="An error message cannot be displayed because an optional resource assembly   containing it cannot be found An error message cannot be displayed because an optional resource assembly containing it cannot be found"
  LineNumber=1
  LinePosition=31
  SourceUri="file:///Windows/Programs/modulos.xml"
  StackTrace:
at System.Xml.XmlTextReaderImpl.Throw()
at System.Xml.XmlTextReaderImpl.Throw()
at System.Xml.XmlTextReaderImpl.CheckEncoding()
at System.Xml.XmlTextReaderImpl.ParseXmlDeclaration()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlLoader.Load()
at System.Xml.XmlDocument.Load()
at System.Xml.XmlDocument.Load()
at ConsoleApplication3.Program.Display()
at ConsoleApplication3.Program.Main()

---------------EDIT--------------

It took me a while but now I have real error reports :)... Here is the actual error.

"System does not support 'iso-8859-1' encoding. Line 1, position 31." 

I've tried saving my file with different encodings but i keep on getting the same error. Any solutions?

---------------EDIT-----------------
I've solved it :D . I was changing the encoding using notepad, but i forgot I had written an encoding tag in the XML file, and that was causing this problem. Stupid mistake. :(

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

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

发布评论

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

评论(2

把昨日还给我 2024-12-11 16:16:30

好吧,实际上您必须与构建 WinCE 操作系统映像的人交谈,以包含“.Net Compact Framework 3.5 本地化字符串资源”,默认情况下您将拥有 en-US,但您也可以选择其他内容,例如:葡萄牙语。该选项位于目录项视图 ->核心操作系统-> Windows 嵌入式紧凑型 ->应用程序和服务开发 -> .NET 紧凑框架 3.5 -> .Net Compact Framework 3.5 -> .Net Compact Framework 3.5 -> .Net Compact Framework 3.5本地化字符串资源

见附图:
Catalog Item View

如果您使用的是操作系统映像,则还必须重新制作 SDK。此后您应该不再出现此错误。

Well, actually you have to talk with the one that built your WinCE OS image to include also the ".Net Compact Framework 3.5 Localized String Resources" by default you will have en-US, but you can select also something else, for example: Portuguese. The option is in Catalog Items View -> Core OS -> Windows Embedded Compact -> Applications and Services Development -> .NET Compact Framework 3.5 -> .Net Compact Framework 3.5 -> .Net Compact Framework 3.5 Localized String Resources

See attached picture:
Catalog Item View

You will have to remake the OS image and also the SDK if you are using one. After this you should no longer have this error.

掩饰不了的爱 2024-12-11 16:16:30

你真的连接了调试器吗?我之所以这么问,是因为您没有可用的异常消息。当您调试时,它们会自动部署(CAB 文件是 NETCFv35.Messages.EN.cab,位于 C:\Program Files (x86)\Microsoft.NET\SDK\CompactFramework\v3.5\WindowsCE\Diagnostics< /代码>)。

我们所知道的是,它是一个 XmlException,因此它与文件位置或被锁定无关(在这种情况下,您会得到不同类型的异常)。您尝试加载的文件中很可能存在无效或格式错误的 XML。您必须发布 XML 本身才能准确了解,但作为测试,只需将您的文件替换为仅包含定义和根节点的简单 XML 文件,然后看看会发生什么。

Are you actually attached with the debugger? I ask becasue of the fact that you don't have exception messages available. Those get deployed automatically when you are debugging (the CAB file is NETCFv35.Messages.EN.cab found at C:\Program Files (x86)\Microsoft.NET\SDK\CompactFramework\v3.5\WindowsCE\Diagnostics).

What we do know is that it's an XmlException, so it has nothing to do with the file location or it being locked (you'd get a different type of exception is that case). Most likely you have invalid or malformed XML in the file you're trying to load. You'd have to post the XML itself for use to know exactly, but as a test, simply replace your file with a simple XML file containing just a definition and a root node and see what happens.

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