在XElement中获取空格

Tod*_*ain 5 .net vb.net xelement whitespace linq-to-xml

我知道这有点愚蠢,但我正在转换的XML有时会有一个只有单个或双个空格的元素.像这样:

Dim es1 = <Text> </Text>
Run Code Online (Sandbox Code Playgroud)

当我试图得到.Value这个时Dim resultText = es1.Value,它只是一个空字符串.如果元素中有前导和/或尾随空格以及至少一个其他字符,则这不是问题.

反正有强迫.Value给我空白区域,如果那就是全部?

Jon*_*eet 6

LoadOptions.PreserveWhitespace解析XML时使用.C#示例代码:

using System;
using System.Xml.Linq;

class Test
{
    static void Main()
    {
        string xml = "<Foo> </Foo>";

        XElement withWhitespace = XElement.Parse(xml,
            LoadOptions.PreserveWhitespace);
        Console.WriteLine(withWhitespace.Value.Length); // Prints 1
        XElement withoutWhitespace = XElement.Parse(xml);
        Console.WriteLine(withoutWhitespace.Value.Length); // Prints 0

    }
}
Run Code Online (Sandbox Code Playgroud)

(显然使用时,这是可用的Load,以及Parse等)

我不知道如何适应VB XML文字,但我会假设通常你实际上是从文件中解析等:)