- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有以下 XML 文件,目前有 100 多个客户端节点,我想使用 C# 向每个节点添加元素。
我的 XML 文件结构如下:
<file_specs>
<client>
<account_number></account_number>
<client_name></client_name>
<file_type></file_type>
<file_extension></file_extension>
<file_hasdelimiter></file_hasdelimiter>
<file_delimiter></file_delimiter>
<central_one>false</central_one>
<central_code>none</central_code>
<central_two>false</central_two>
<c_two_code>none</c_two_code>
<header_line>true</header_line>
<has_quotes>true</has_quotes>
<start_line>1</start_line>
<has_one>true</has_one>
<one_column>2</one_column>
<has_two>true</has_two>
<two_column>12</two_column>
</client
我查看了其他答案,并尝试了各种解决方案。这个有效,但仅适用于第一个客户,所有其他客户均不受影响:
XDocument doc = XDocument.Load(@"c:/xmlconfig/sample.xml");
doc.Root.Element("client").Add(new XElement("testing", "none"));
我尝试添加一个 foreach 循环,它为每个客户端节点添加了一个测试元素,但它会将所有这些元素添加到第一个条目中,而其余的都保持不变。
XDocument doc = XDocument.Load(@"c:/xmlconfig/miss.xml");
foreach (var client in doc.Descendants("client"))
{
doc.Root.Element("client").Add(new XElement("testing", "none"));
}
我哪里遗漏了什么?
最佳答案
您应该向每个客户端添加新元素:
XDocument doc = XDocument.Load(@"c:/xmlconfig/miss.xml");
foreach (var client in doc.Descendants("client"))
{
// use current client
client.Add(new XElement("testing", "none"));
}
让我们看看这里发生了什么:
foreach (var client in doc.Descendants("client"))
{
doc.Root.Element("client").Add(new XElement("testing", "none"));
}
对于每个客户端,您执行查询,该查询在根目录下获取第一个客户端元素。然后将新元素添加到第一个客户端。这重复了很多次,就像 xml 中的客户端数量一样。你最终测试元素添加到第一个客户端 N 次。
关于c# - 向现有 XML 文件中的多个节点添加新元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23657935/
我正在用 Java 编写代码,并且使用 Vaadin 8 扩展。 我有一个 Vaadin 组合盒,效果很好。但我不仅想从组合框中选择项目,还想选择书面输入。这意味着我想使用组合框作为下拉菜单以及文本编
我正在尝试将 AJAX 添加到 JQuery ListView 中并呈黄色闪烁,但我似乎无法使其正常工作。谁能指出我正确的方向? http://jsfiddle.net/zFybm/ 最佳答案 根据
我有这个样式表: .pixel{ position: absolute; height: 10px; width: 10px; background-color: #f
这是我用来将新行推送到容器的一行代码: this.$el.append(new ItemView(item).render().el); 其中item是Backbone.js model,render
我正在尝试在 anguar.js 中制作一些测试应用程序,但遇到了问题。我的 js 文件包含: live = angular.module('live',[]); live.controller('p
如何绑定(bind)页面加载后创建的新元素? 我有这样的东西 system = function() { this.hello = function() { alert
html5 新元素(页眉、导航、页脚等)在 IE 中不工作 最佳答案 您需要包含 HTML5 shiv 脚本以允许在旧版 IE 浏览器中设置 HTML5 元素的样式:http://code.googl
我是一名优秀的程序员,十分优秀!