问题标签 [xdoc]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
91 浏览

c# - 如何在 .Net 中使用 XDocument 迭代 XML

我有一个很大的 XML 文件,我在其中使用小片段ReadFrom(),然后我将获得不同位置的xmlsnippet包含leaf、、、标签(有时与 kir 相比saskir叶子在顶部,反之亦然)。

现在问题是我正在使用三个foreach循环来获取这些值,这是错误的逻辑,并且当这个片段也很大时需要时间。

无论如何我可以使用一个foreach循环然后if loop在 foreach 中使用三个循环来获取值吗?

arr是一种习俗arraylist

0 投票
1 回答
26 浏览

xml - 如何在加载和保存之间保留 XDocument 的缩进?

我有一个格式如下的 XDocument:

加载并保存后更改为以下格式:

如何保留上述格式?

如果我SaveOptions在保存期间使用,则没有缩进并且整个文件在一行中。我希望保留上述格式。可能吗 ?

0 投票
0 回答
36 浏览

c# - 在 C# 中更新两个相似类型的 XML 文件的节点值

我在我的应用程序中从外部源接收两个未知格式的 xml 文件。通常两个 xml 文件具有相同的结构,但有时某些节点是不同的。我必须逐个节点读取一个 xml 文件并更新另一个 xml 文件中类似位置的值。在这里,我想更新文件 1 中的文件 2 值,并且还想在文件 2 中找到任何额外的节点。如何实现这一点?

文件 1:

文件 2:

输出:

0 投票
2 回答
904 浏览

c# - 使用LINQ查询XDocument,如何获取具体值?

我正在尝试重构以下内容-可行,但是如果我开始在 XML 中获取更多元素,它将变得难以管理:

给定以下 XML URL

理想情况下,我想删除循环和 if 语句,并有一个 LINQ 查询,它以一种干净的方式从 XML 中只返回我需要的字段(id、可用性等),并用这些数据填充一个简单的类。

任何人都可以帮忙吗?

0 投票
1 回答
22 浏览

c# - 反转一些孩子后如何保存xml文件

我有一个 xml 文件,我想反转一些节点,然后我想保存结果。我使用了 XDocumnt 并在其上使用了 revese 函数,它正确地反转了,但之后我不知道如何保存文件。像下面这样的原始文件

我需要将其反转如下:

之后,我想将其保存到新文件中。

谁能帮我?

0 投票
1 回答
214 浏览

c# - 使用 XElement 在 c# 中循环每个项目

我在交换C# 中 sid列表中的属性时遇到了一些困难。是参考。ItemXDocumentid

输入:

预期输出:

有一个标签<Ref>,其中一部分 value是1st 和 2nd的Base属性的ex:bookref1引用。book_bookref1_ref1 Item

我需要将第二个“id”Item写入第一个并将第一个写入循环中id的第二个。Item同样适用于 3rd 和 4th Item。可以有很多,但它就像一个组合(1 和 2、3 和 4、5 和 6 等)。

这里的问题是可以有多个s 并且映射属性Item中没有关系。ItemId

我正在使用的代码正在加载XMLDocument到LINQ 中XDocument获取Items 。

请让我知道你的建议。

0 投票
1 回答
416 浏览

c# - 如何获取 xml 文档的所有命名空间以进行正确的 XPath 评估?

我尝试评估简单的 xpath

对于这个 xml

但我在运行时得到 xml 文件,即我无法使用 AddNamespace() 方法填充 XmlNamespaceManager(这就是评估失败的原因)。我试着做这个把戏

但这对我没有帮助。您知道如何为 XPath 解析名称空间或以其他方式评估 XPath 吗?谢谢!

0 投票
1 回答
78 浏览

c# - 无法将字符串转换为 XElement

我正在创建一个 xml 文件,但无法转换标签中的字符串<Nb>

如何将其转换为 XElement 项?如果可能的话,我想避免使用 Linq、XmlDoc ......

0 投票
1 回答
211 浏览

c# - Remove all text not wrapped in XML braces

I want to remove all invalid text from an XML document. I consider any text not wrapped in <> XML brackets to be invalid, and want to strip these prior to translation.

From this post Regular expression to remove text outside the tags in a string - it explains how to match XML brackets together. However on my example it doesn't clean up the text outside of the XML as can be seen in this example. https://regex101.com/r/6iUyia/1

I dont think this specific example has been asked on S/O before from my initial research.

Currently in my code, I have this XML as a string, before I compose an XDocument from it later on. So I potentially have string, Regex and XDocument methods available to assist in removing this, there could additionally be more than one bit of invalid XML present in these documents. Additionally, I do not wish to use XSLT to remove these values.

One of the very rudimentary idea's I tried and failed to compose, was to iterate over the string as a char array, and attempting to remove it if it was outside of '>' and '<' but decided there must be a better way to achieve this (hence the question)

This is an example of the input, with invalid text being displayed between nested-A and nested-B

I expect the output to be in a format like the below.

0 投票
1 回答
70 浏览

c# - xml 子元素节点或属性值

我有一个带有嵌套 xml 元素的 xml 架构,以下是其中的一小部分

从中,我必须得到“aa tag”下的“name tag”的值,电信标签的最后一个属性(tel :),以及“value tag”的属性值(在bb标签和cc标签下找到)

我尝试了以下代码,但它并没有完全达到我的预期。

请提供此问题的解决方案。