- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我的目标是输出修改后的 XML 文件并保留原始文件中存在的特殊缩进。目的是使生成的文件看起来仍然像原始文件,使它们更容易通过源代码管理进行比较和合并。
我的程序将读取 XML 文件并添加或更改一个特定属性。
这是我试图实现/保留的格式:
<Base Import="..\commom\style.xml">
<Item Width="480"
Height="500"
VAlign="Center"
Style="level1header">
(...)
在这种情况下,我只是希望将第一个属性之后的所有属性与第一个属性对齐。
XmlWriterSettings
提供了格式设置选项,但它们达不到我正在寻找的结果。
settings.Indent = true;
settings.NewLineOnAttributes = true;
这些设置会将第一个属性放在换行符上,而不是将其与节点放在同一行,并将属性与节点对齐。
这是 Load
调用,要求保留空格:
MyXml = XDocument.Load(filepath, LoadOptions.PreserveWhitespace);
但它似乎并没有达到我的预期。
我试图提供一个自定义类,它派生自 XmlWriter
到 XDocument.Save
调用,但我没有设法正确插入空格而没有遇到 InvalidOperationException
.此外,该解决方案对于我正在寻找的小添加物来说似乎有些矫枉过正。
作为引用,这是我的保存调用,没有使用我的自定义 xml 编写器(无论如何都不起作用)
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.NewLineOnAttributes = true;
settings.OmitXmlDeclaration = true;
using (XmlWriter writer = XmlWriter.Create(filepath + "_auto", settings))
{
MyXml.Save(writer);
}
最佳答案
我最终没有完全使用 XDocument.Save,而是创建了一个采用 XDocument、XmlWriter 和 TextWriter 的类。该类解析 XDocument 中的所有节点,TextWriter 绑定(bind)到磁盘上的文件,XmlWriter 将其用作其输出管道。
我的类然后使用 XmlWriter 输出 xml。为了获得额外的间距,我使用了此处描述的解决方案,https://stackoverflow.com/a/24010544/5920497 ,这就是我还使用底层 TextWriter 的原因。
这是解决方案的示例。
调用类保存文档:
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.NewLineOnAttributes = false; // Behavior changed in PrettyXmlWriter
settings.OmitXmlDeclaration = true;
using(TextWriter rawwriter = File.CreateText(filepath))
using (XmlWriter writer = XmlWriter.Create(rawwriter, settings))
{
// rawwriter is used both by XmlWriter and PrettyXmlWriter
PrettyXmlWriter outputter = new PrettyXmlWriter(writer, rawwriter);
outputter.Write(MyXml);
writer.Flush();
writer.Close();
}
PrettyXmlWriter 内部:
private XmlWriter Writer { get; set; }
private TextWriter InnerTextWriter { get; set; }
public void Write(XDocument doc)
{
XElement root = doc.Root;
WriteNode(root, 0);
}
private void WriteNode(XNode node, int currentNodeDepth)
{
if(node.NodeType == XmlNodeType.Element)
{
WriteElement((XElement)node, currentNodeDepth);
}
else if(node.NodeType == XmlNodeType.Text)
{
WriteTextNode((XText)node, currentNodeDepth, doIndentAttributes);
}
}
private void WriteElement(XElement node, int currentNodeDepth)
{
Writer.WriteStartElement(node.Name.LocalName);
// Write attributes with indentation
XAttribute[] attributes = node.Attributes().ToArray();
if(attributes.Length > 0)
{
// First attribute, unindented.
Writer.WriteAttributeString(attributes[0].Name.LocalName, attributes[0].Value);
for(int i=1; i<attributes.Length; ++i)
{
// Write indentation
Writer.Flush();
string indentation = Writer.Settings.NewLineChars + string.Concat(Enumerable.Repeat(Writer.Settings.IndentChars, currentNodeDepth));
indentation += string.Concat(Enumerable.Repeat(" ", node.Name.LocalName.Length + 1));
// Using Underlying TextWriter trick to output whitespace
InnerTextWriter.Write(indentation);
Writer.WriteAttributeString(attributes[i].Name.LocalName, attributes[i].Value);
}
}
// output children
foreach(XNode child in node.Nodes())
{
WriteNode(child, currentNodeDepth + 1);
}
Writer.WriteEndElement();
}
关于c# - 如何使用 XDocument.Save 使用属性的自定义缩进保存文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44726344/
我有一个将 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
我是一名优秀的程序员,十分优秀!