【问题标题】:Xml database not workingXML 数据库不工作
【发布时间】:2023-03-04 19:30:01
【问题描述】:

我正在创建一个 Windows Phone 应用程序,我需要实现一个相当大的数据库(由 288,000 个项目组成),但每当我尝试运行它时,程序都会给我一个System.Xml.XmlException

问题是,如果我只在 XML 数据库中放入 100 个或更少的项目,它就可以工作,并允许我查询它。

你知道它为什么会这样吗?

代码如下:

loadCustomData = XDocument.Load("vocabolario.xml");

var domanda = from c in loadCustomData.Descendants("Parola")
              where c.Attribute("id").Value == "1"
              select c.Attribute("Contenuto").Value;

lol.Text = domanda.First();

【问题讨论】:

  • 可能是当您调用 XDocument.Load 时,由于 XDocument.Load 会在内存中加载完整文件
  • 您应该使用 SQLite 之类的数据库来处理 288000 个项目
  • @Haris Hasan 是的,你是对的。 user1868607应该选择DB,可以通过简单的查询来检索数据,不必将整个数据加载到内存中
  • @user1868607 您可以使用上述方法或使用 XmlReader 来达到目的

标签: c# xml windows windows-phone


【解决方案1】:

来自 Jon Skeet 的文章。

如果您乐于将所有内容读入内存,请使用 XDocument。它会让你的生活更轻松。 LINQ to XML 是一个可爱的 API。

如果您需要以流方式处理大型 XML 文件,基本上可以使用 XmlReader(例如 XmlTextReader)。这是一个更痛苦的 API,但它允许流式传输(即只处理您需要的数据,因此您可以浏览一个巨大的文档并且一次只有少量内存)。

但是有一种混合方法 - 如果您有一个由小元素组成的大型文档,您可以从位于元素开头的 XmlReader 创建一个 XElement,使用 LINQ to XML 处理该元素,然后移动XmlReader 放到下一个元素上并重新开始。

Jon Skeet Description

Check the Example below
   // Create a DOM document with some content.
   XmlDocument doc = new XmlDocument();
   XmlElement child = doc.CreateElement("Child");
   child.InnerText = "child contents";
   XmlElement root = doc.CreateElement("Root");
   root.AppendChild(child);
   doc.AppendChild(root);

   // Create a reader and move to the content.
   using (XmlNodeReader nodeReader = new XmlNodeReader(doc)) {
   // the reader must be in the Interactive state in order to
   // Create a LINQ to XML tree from it.
   nodeReader.MoveToContent();

   XElement xRoot = XElement.Load(nodeReader);
   Console.WriteLine(xRoot);
}

这只是一个例子,尝试类似的事情。

【讨论】:

  • 实际上是一个由小元素组成的巨大文档……您有混合解决方案的示例吗?
  • @user1868607 请检查更新。
猜你喜欢
  • 2017-07-20
  • 1970-01-01
  • 2017-01-12
  • 2016-11-08
  • 2017-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多