Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以XmlDocument样式以编程方式创建XmlNode问题_C#_.net_Xml_Forms - Fatal编程技术网

C# 以XmlDocument样式以编程方式创建XmlNode问题

C# 以XmlDocument样式以编程方式创建XmlNode问题,c#,.net,xml,forms,C#,.net,Xml,Forms,我在将Xml输出获取到预期的文件时遇到问题 我想要这个: 但我得到的是: 我创建节点的代码是: XmlNode newNode = _xmlDocument.CreateElement("Book"); //Add Attributes XmlAttribute attrID = _xmlDocument.CreateAttribute("id"); attrID.Value = newBook.ID; newNode.Attributes.Append(attrID); XmlAttr

我在将Xml输出获取到预期的文件时遇到问题

我想要这个:

但我得到的是:

我创建节点的代码是:

XmlNode newNode = _xmlDocument.CreateElement("Book");
//Add Attributes
XmlAttribute attrID = _xmlDocument.CreateAttribute("id");
attrID.Value = newBook.ID;
newNode.Attributes.Append(attrID);

XmlAttribute attrDesc = this._xmlDocument.CreateAttribute("desc");
attrDesc.Value = newBook.Description;
newNode.Attributes.Append(attrDesc);

XmlAttribute attrMmType = this._xmlDocument.CreateAttribute("mmtype");
attrMmType.Value = newBook.mmType;
newNode.Attributes.Append(attrMmType);

//Add new child node to parent
parentNode.InsertBefore(newNode, parentNode.FirstChild);
我不知道如何以不同的方式设置节点以获得块结果。 也许我需要在保存节点之前将其添加到子节点?但是我没有 对于此特定节点

任何帮助都将不胜感激。

的缩写,其中打开/关闭部分之间没有任何内容


您可以通过添加一个
XmlText
,将空格作为元素的子元素来解决这个问题,但要意识到,从长远来看,这是毫无意义的,因为您只是以一种非最佳的方式直观地格式化它。任何像样的XML解析器都应该能够读取短格式标记,因为这是推荐的行为。

当没有子节点时,为什么需要这样的标记?我真的很好奇。我想这只是为了反映过去xml文件的使用情况。我正在编辑现有的xml文件,希望它看起来一样。回顾现有文件,我发现没有没有没有子节点的节点。所以我想我需要研究一下。谢谢你,我知道这是一种完全可以接受的标记形式,只是想知道我是否在代码上做了什么错误。看起来这只是有孩子和没有孩子之间的区别。你的回答恰到好处。