- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我一直致力于以 XML 格式存储数据,但我使用了一个级别系统来允许用户访问某些数据而不是其他数据。但是这种情况发生在不同的周期并且数据被读取和写入是不规则的。这给了我以下错误:
Additional information: This operation would create an incorrectly structured document.
在:
doc.Add(new XElement(UserLevel, new XElement(CommandName.Remove(0, 1), CommandInfo)));
这是完整的函数:
private bool SetCommands(string CommandName, string CommandInfo, string UserLevel)
{
if (GetCommand(CommandName) == "none")
{
XDocument doc = new XDocument();
if (File.Exists(XmlFileLocation))
doc = XDocument.Load(XmlFileLocation);
doc.Add(new XElement(UserLevel, new XElement(CommandName.Remove(0, 1), CommandInfo)));
doc.Save(XmlFileLocation);
return true;
}
else
{
return false;
}
}
我想要的是能够在相同的 UserLevel 下使用不同的 CommandNames 写入文件,然后保存不同的 CommandInfos。稍后我计划能够编辑 CommandInfo,因此我将不得不覆盖已写入的内容。
我在找什么?
最佳答案
一个 XML 文档只能有一个根元素,而您似乎正试图添加多个。只需创建一个顶级元素,比如 Users
,然后添加 UserLevel
作为其子元素。
像这样:
private bool SetCommands(string CommandName, string CommandInfo, string UserLevel)
{
if (GetCommand(CommandName) == "none")
{
XDocument doc = new XDocument();
if (File.Exists(XmlFileLocation))
doc = XDocument.Load(XmlFileLocation);
var users = doc.Root.Element("Users");
if (users == null)
{
users = new XElement("Users");
doc.Add(users);
}
users.Add(new XElement(UserLevel, new XElement(CommandName.Remove(0, 1), CommandInfo)));
doc.Save(XmlFileLocation);
return true;
}
else
{
return false;
}
}
关于c# - 使用 XDocument 不规则写入 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31214957/
我有一个将 XDocument 对象作为参数的函数。 我需要遍历不同集合中的许多其他对象,并针对这些对象中的每一个,在 XDocument 上执行一些操作。但是 lopp 的每次迭代都需要传递给函数的
下面的代码可以工作,但是很乱而且很慢。我正在使用带有 Saxon 的 XSLT2 将一个 XDocument 转换为另一个 XDocument,使用 SaxonWrapper 进行改编: public
我在 Visual Studio 2010 的 MVC4 Web API 应用程序中使用 XDocument,但不确定测试策略。 我的大部分单元测试都使用内存中的 XDocument,它适用于 Con
我无法使用 Linq 加载新的 XML 文档。那是我的代码: using System; using System.Collections.Generic; using System.Linq.Exp
抱歉,如果我的问题不清楚。我从 xDocument 中获得了一堆元素和每个元素的属性。现在我希望在每次迭代中获得每个节点的更多后代并遍历它们并获取它们的所有属性。 结构:
我接管了一个以二进制格式在 SQL Server 中存储大型 XML 文档的系统。 目前,数据是通过将其转换为字符串,然后将该字符串转换为字节数组来保存的。但最近在尝试转换为字符串时遇到一些大型 XM
我有一个使用 XDocument 构建 xml 文档的小应用程序。但是,一段时间后,该应用程序使用了超过 1GB 的内存。 所以我想知道是否有办法让 XDocument 使用磁盘而不是内存。例如,通过
我有一个像这样的 XDocument: 1057 Lookup_test open 1057 Lookup_test Waiting for input
如何将 xml 元素导入到对象?我下面的代码不起作用,它在 SetValue 处失败,我不明白为什么。 但即便如此,我怀疑 linq 有一种更简洁的方法来执行此操作,但我找不到任何示例。 class
我多年来一直通过 API 访问数据库,今天进行了更改,但我无法联系所有者。这似乎是一个微妙的变化,导致我的代码给出空引用异常。下载一个文件,然后我尝试使用 XmlReader 并使用以下代码将其加载到
我正在尝试将 XDocument 反序列化为包含数组/列表的类。问题是这段代码没有这样做,我总是得到一个没有任何库存项目的 Response 类(不是 null,但计数 = 0) 这是我的测试方法:
我正在使用 XDocument 来保存某种数据库。这个数据库由注册的聊天机器人组成,我只是有许多具有“用户名”、“所有者”等属性的“机器人”节点。然而,偶尔一些聪明人决定制作一个具有非常奇怪角色作为属
我有一个简化后的 xml 文档,如下所示: 有一个\t作为属性值。 执行这段代码时 var path = @"C:\test.xml"; var doc = XDocument.Load(path
当我尝试使用下一个代码加载一些 XML 文件时: XDocument configuration = XDocument.Load("%NAME_OF_THE_PATH%/Default_Te
我已经完成了研究并尝试了很多次试验,但这让我感到难过。我可能有点笨,但是鉴于以下 XML 文档,我怎样才能最轻松地找到 UnitTestResult 元素?
我需要将多个 namespace 添加到具有相同地址的 xdocument (C#) stuff and more stuff 如果我使用下面的代码添加..它只显示 xmlns:f XNames
如何从加载的 XDocument 中删除特定节点?我的 XML 文档如下所示: if (condition) { } ...
我有一个类似于 的 XDocument 我希望将属性 foo
很容易找到 Xdocument 属性的值,但是我们如何找到带有前缀/命名空间的属性。 XML 代码 My value 我正在尝试读取属性值 xlink:Id(其中 p 是 XElement) p =
我有以下 XML 文档: 我正在尝试使用 XDocument 解析此 XML,以便获得包含节点及其值的列表(字典): source => mysource,name
我是一名优秀的程序员,十分优秀!