xml元素值需要保留space

XmlReaderSettings.IgnoreWhitespace 属性

如果忽略空白,则为 true;否则为 false。 默认值为 false。

示例

下面创建一个设置对象,该对象可用于构造一个读取器,该读取器去除处理指令、注释和微不足道的空白。

    StreamReader textreader = new StreamReader(filterXML);
    XmlReader reader = default(XmlReader);
    reader = XmlReader.Create(textreader, new XmlReaderSettings
    {
        CloseInput = true,
        ConformanceLevel = ConformanceLevel.Document,
        DtdProcessing = DtdProcessing.Ignore,
        IgnoreComments = true,
        IgnoreProcessingInstructions = false,
        IgnoreWhitespace = true,
        ValidationType = ValidationType.None
    });

注解

不被视为重要的空格包括空格、制表符和空白行,用于设置标记,以便提高可读性。 其中一个示例是元素内容中的空白。
此属性设置不会影响混合内容模式下标记之间的空白,也不会影响在属性范围内 xml:space=‘preserve’ 发生的空白。

<idPkg:Story xmlns:idPkg="http://ns.adobe.com/AdobeInDesign/idml/1.0/packaging" DOMVersion="17.0">
	<Story Self="u236" AppliedTOCStyle="n" UserText="true" IsEndnoteStory="false" TrackChanges="false" StoryTitle="$ID/" AppliedNamedGrid="n">
		<StoryPreference OpticalMarginAlignment="false" OpticalMarginSize="12" FrameType="TextFrameType" StoryOrientation="Horizontal" StoryDirection="LeftToRightDirection" />
		<InCopyExportOption IncludeGraphicProxies="true" IncludeAllResources="false" />
		<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/10 Notes Text">
			<CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/[No character style]">
				<Properties>
					<Leading type="unit">13.5</Leading>
					<AppliedFont type="object">CompositeFont/~MHeHK-B+TimLTS-B</AppliedFont>
				</Properties>
				<Content> </Content>
			</CharacterStyleRange>
		</ParagraphStyleRange>
	</Story>
</idPkg:Story>
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
exclude-result-prefixes="msxsl"  xmlns:space="http://ns.adobe.com/AdobeInDesign/idml/1.0/packaging">
    <xsl:output method="xml" indent="yes"/>
    <!-- This is an identity template - it copies everything
         that doesn't match another template -->
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
  <!-- This is the "other template". It says to use your BBB-DDD elements
       instead of the AAA element -->
   <xsl:template match="Content[text()=' ']">
    <xsl:element name="Content">
        <xsl:attribute name="xml:space">preserve</xsl:attribute>
        <xsl:value-of select="."></xsl:value-of>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

将上面的xml通过xsl转化成带有xml:space=‘preserve’ 的空值元素,新的xml

<Content xml:space="preserve"> </Content>
private string TransformXml(string xmlPath)
{
	string transformedXmlPath = System.IO.Path.GetTempFileName();
	XslCompiledTransform xslt = new XslCompiledTransform();
	string xsltFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "filterSpaceXslt.xslt");
	xslt.Load(xsltFilePath);
	xslt.Transform(xmlPath, transformedXmlPath);
	return transformedXmlPath;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值