2016-05-30 12 views
2

は、私は次の単純なXMLファイルを持っている:XmlReader.IsEmptyElementが異なる値を返すのはなぜですか?

<?xml version="1.0" encoding="UTF-8" ?><work><pageSetup paperSize="9" fitToHeight="0" orientation="landscape"></pageSetup></work> 

私は次のコードを実行すると:

using (XmlReader reader = XmlReader.Create(inFile)) 
    while (reader.Read()) 
     Console.WriteLine("Name = {0}, NodeType = {1}, IsEmptyElement ={2}\n", reader.Name, reader.NodeType, reader.IsEmptyElement); 

出力は次のとおりです。

名= XML、ノードタイプ= XMLDECLARATION、IsEmptyElement = False

名前= work、NodeType =要素、IsEmptyElement = False

名=のPageSetup、ノードタイプ=要素、IsEmptyElement = Falseの

名=のPageSetup、ノードタイプ=のendElement、IsEmptyElement = Falseの

名=仕事、ノードタイプ=のendElement、IsEmptyElement = Falseの


あなたが表すPageSetupのIsEmptyElementは= Falseを見ることができるように(私はなぜ知らない... https://msdn.microsoft.com/en-us/library/system.xml.xmltextreader.isemptyelement.aspxを参照)

しかし、私はラインとなり、XML(メモ帳でCtrl + Alt +シフト+ Bを++)foramt場合が壊れる:

<?xml version="1.0" encoding="UTF-8" ?> 
<work> 
    <pageSetup paperSize="9" fitToHeight="0" orientation="landscape"/> 
</work> 

し、プログラムを実行し、出力は次のようになります。

名= XML、ノードタイプ= XMLDECLARATION、IsEmptyElement = Falseの

名=、ノードタイプ=空白、IsEmptyElement = Falseの

名=仕事、ノードタイプ=要素、I sEmptyElement = Falseの

名=、ノードタイプ=空白、IsEmptyElement = Falseの

名=のPageSetup、ノードタイプ=要素、IsEmptyElement = Trueの

名=、ノードタイプ=空白、IsEmptyElement = Falseの

名=仕事、ノードタイプ=のendElement、IsEmptyElement = Falseの


pageSetupのIsEmptyElement = Trueを参照してください。

なぜ2つのXMLファイルの間に(pageSetupのIsEmptyElementの値で)異なるのですか?

+0

改行のためではありません。これは、あるケースでは、「」と、もう1つは「」です。 – sstan

+0

おそらく前者は空のテキストノードを含んでいますか? –

答えて

2

MSDNによると、IsEmptyElementは、ソースドキュメントの要素に終了要素タグがあるかどうかを単に報告します。最初のケースで

は、端部要素を第二の場合と同様に、あなたはtrueIsEmptyElementセットを見る理由はない終了タグを持っていないので、IsEmptyElement戻りfalseを(要素の内容が空であるが)、持っています。

関連する問題