GVKun编程网logo

c# – 使用PreserveWhitespace LoadOptions在XDocument中添加带有新行的新XElements

14

在这里,我们将给大家分享关于c#–使用PreserveWhitespaceLoadOptions在XDocument中添加带有新行的新XElements的知识,同时也会涉及到如何更有效地.NETXML

在这里,我们将给大家分享关于c# – 使用PreserveWhitespace LoadOptions在XDocument中添加带有新行的新XElements的知识,同时也会涉及到如何更有效地.NET XMLDocument.PreserveWhitespace:如何保留重要的空格?、CM: Create BAdI implementation for Webservice enabled word document、Document Builder: how does document element and variable cooperate、document.all还是document.getElementsByName的内容。

本文目录一览:

c# – 使用PreserveWhitespace LoadOptions在XDocument中添加带有新行的新XElements

c# – 使用PreserveWhitespace LoadOptions在XDocument中添加带有新行的新XElements

我正在尝试编辑保存其格式的 XML文件:
<root>
    <files>
        <file>a</file>

        <file>b</file>
        <file>c</file>



        <file>d</file>
    </files>
</root>

所以我使用XDocument xDoc = XDocument.Load(path,LoadOptions.PreserveWhitespace)加载xml文档;
但是当我试图添加新元素xDoc.Root.Element(“files”)时.添加(new XElement(“test”,“test”));
xDoc.Root.Element(“files”).Add(new XElement(“test2”,“test2”));
它添加在同一行,因此输出如下:

<root>
    <files>
        <file>a</file>

        <file>b</file>
        <file>c</file>



        <file>d</file>
    <test>test</test><test2>test2</test2></files>
</root>

那么如何在新行中添加新元素以节省初始格式?我尝试使用带有Setting.Indent = true的XmlWriter来保存XDocument,但正如我所见,当我使用xDoc.Root.Element()时,元素被添加到同一行.Add()

更新:程序加载,修改和保存文档的完整部分

using System;
using System.Xml;
using System.Xml.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {            
            string path = @".\doc.xml";    
            XDocument xDoc = XDocument.Load(path,LoadOptions.PreserveWhitespace);

            //when i debug i see in "watch" that after these commands new elements are already added in same line
            xDoc.Descendants("files").First().Add(new XElement("test","test"));
            xDoc.Descendants("files").First().Add(new XElement("test2","test2"));

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Encoding = Encoding.UTF8;
            settings.Indent = true;
            settings.IndentChars = "\t";

            using (XmlWriter writer = XmlTextWriter.Create(path,settings))
            {                
                xDoc.Save(writer);
                //Here i also tried save without writer - xDoc.Save(path)
            }
        }
    }
}

解决方法

问题似乎是由您使用LoadOptions.PreserveWhitespace引起的.这似乎胜过XmlWriterSettings.Indent – 你基本上说过,“我关心这个空白”……“哦,现在我没有.”

如果删除该选项,只需使用:

XDocument xDoc = XDocument.Load(path);

…然后适当地缩进.如果你想保留所有原始空格,但只是缩进新元素,我想你需要自己添加缩进.

.NET XMLDocument.PreserveWhitespace:如何保留重要的空格?

.NET XMLDocument.PreserveWhitespace:如何保留重要的空格?

如何解决.NET XMLDocument.PreserveWhitespace:如何保留重要的空格??

简单来说,我有一个这样的 XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?><root> <data> </data></root>

如你所见,根条目标签后有一个空格,数据标签中有一个空格。当我现在这样使用它时:

var xmldoc = new XmlDocument();
xmldoc.Load(stream);

...文档丢失了两个空格,根条目标签之后的一个和数据标签中的一个。

但是如果我这样使用它:

var xmldoc = new XmlDocument();
xmldoc.PreserveWhitespace = true;
xmldoc.Load(stream);

...然后保留两个空格。

但根据文档(documentation,备注部分),默认设置 PreserveWhitespace = false 应该保留重要的空格并去除不重要的空格。但它清除了所有这些,数据标签中的一个很重要。还是我理解错了?

解决方法

重要的空格是文本之间的空格,例如“这是一个示例”,但像您描述的空标签并不重要,将被 xml 解析器删除。

CM: Create BAdI implementation for Webservice enabled word document

CM: Create BAdI implementation for Webservice enabled word document

Created by Jerry Wang, last modified on Oct 16, 2014

参考这篇document给product master 或者其他business transaction 创建word格式的attachment,并且这些word document能够支持web service:

我们可以将Genil model里支持web service的node上的field直接在word application里从右边的树状结构里拖拽到word左边的编辑区域:

 

clipboard1

 

 

当在UI上给product创建word attachment时,系统会自动call Genil Model PROD暴露出来的web service取得当前product instance的值并且赋给wrod document里对应的字段。系统提供了一个BAdI enhancement spot允许客户自定义:

 

clipboard2

 

 

 

clipboard3

 

 

 

BAdI的filter value设置成webservice的technical name:

clipboard4

 

可以利用该BAdI implementation 处理一些默认被Web service framework 过滤掉的属性为hidden的字段,如product的UPNAME:

clipboard5

 

 

 

clipboard6

本文同步分享在 博客“汪子熙”(CSDN)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

Document Builder: how does document element and variable cooperate

Document Builder: how does document element and variable cooperate

Created by Jerry Wang, last modified on Nov 03, 2014

In customizing, we can select one element and maintain a rule for it in order to achieve that the element would only appear in the final document once some condition meets. We could also maintain one or more variables to that element:

 

clipboard1

For example here we have assigned the variable "ZJERRY_TEST" to element ZCR_CD:

 

 

 

clipboard2

 

 

such variable has type CHAR10 and has default value "i042416".

 

clipboard3

 

 

In document template design time, we could drag the variable from right variable tree pane and drop them to the left word layout part:

 

clipboard4

 

 

In the document creation ui, we could input some data to the variable:

 

clipboard5

 

 

and in the runtime we could see the user input is merged to the final rendered document:

 

clipboard6

本文同步分享在 博客 “汪子熙”(CSDN)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与 “OSC 源创计划”,欢迎正在阅读的你也加入,一起分享。

document.all还是document.getElementsByName

document.all还是document.getElementsByName

当页面上的控件同名且多个的时候,你首先做的是什么?判断长度?的确,从程序的严密角度出发,我们是需要判断长度,而且有长度和没长度是两种引用方法.我们来看: oEle= document.all.aaa ;//这里有一个aaa的对象,但我们不知道它现在长度是多少,所以没办法对它操作.因此,我们要先做判断长度的过程.如下:
if(oEle.length){}else{};
登录后复制
在两种情况下,花括号里面的内容写法也是不一样的:
if(oEle.length){ 
   for(var i = 0 ;i<oEle.length;i++){ 
      oEle[i].value........ 
  } 
} 
else{ 
     oEle.value........ 
};
登录后复制
但是这样写是不是太复杂了点?而且当花括号里面的代码比较多的时候,我们要写两遍代码,晕了先~
还好有document.getElementsByName()这个方法.它对一个和多个的处理是一样的,我们可以用:
oEle = document.getElementsByName(''aaa'')来引用
当oEle只有1个的时候,那么就是oEle[0],有多个的时候,用下标法oEle[i]循环获取,是不是很简单?

值得一提的是它对Name和ID的同样有效的.
但是它只能应用到document对象.相对应的,还有另一个方法,可以应用的对象会更广一点:
getElementsByTagName,比如我知道了一个

我要取div里面的所有input,这样写就可以了:aaa.getelementsbytagname(''input''),这样就有效的可以和别的div(比如说有个叫bbb的div,里面的也是一样的input)相区别.
同getelementsbytagname相对应,还有一个document.body.all.tags(),能用这个方法的对象比getelementsbytagname要小得多.但比getelementsbyname要多.
到这里我们还要提一下getelementbyid,它也是只有document对象才能使用,而且返回的是数组的第一个元素,呵呵,它的方法名都写明了是getelement而不是getelements,所以,千万不要搞浑了.

更多相关教程请访问 JavaScript视频教程

我们今天的关于c# – 使用PreserveWhitespace LoadOptions在XDocument中添加带有新行的新XElements的分享就到这里,谢谢您的阅读,如果想了解更多关于.NET XMLDocument.PreserveWhitespace:如何保留重要的空格?、CM: Create BAdI implementation for Webservice enabled word document、Document Builder: how does document element and variable cooperate、document.all还是document.getElementsByName的相关信息,可以在本站进行搜索。

本文标签: