4
using (System.IO.StreamReader sr = new System.IO.StreamReader(path, System.Text.Encoding.GetEncoding("Windows-1252"), true))
{ 

    xdoc = XDocument.Load(sr);
}

ここに私のXMLがあります

<sheet name="sheet1" bounds="160,128,464,288">
    <text name="text1" bounds="160,128,464,288" text="a

    b"/>
</sheet>

XDocument.Load 変換

a

b

a b

改行を保持する方法は?

4

1 に答える 1

5

Whitespaces in attributes are normalized by default to be converted to spaces. It is much safer to have text with new lines in elements instead of attributes.

If it is out of your control - setting XmlTextReader.Normalization to false should prevent default behavior.

Partial sample from the article below:

// Create the XML fragment to be parsed. 
string xmlFrag  = 
@"<item attr1='  test A B C
    1 2 3'/>
  <item attr2='&#01;'/>";                         
XmlTextReader reader = new XmlTextReader(xmlFrag, XmlNodeType.Element, context);

// Show attribute value normalization.
reader.Read();
reader.Normalization = false;
Console.WriteLine("Attribute value:{0}", reader.GetAttribute("attr1"));
reader.Normalization = true;
Console.WriteLine("Attribute value:{0}", reader.GetAttribute("attr1"));
于 2013-03-07T23:16:09.823 に答える