What do I need to do in either my C# code or my XML document so that the XDocument parser reads literal whitespace for Value
s of XElement
s?
I have an XML document, part of which looks like this:
<NewLineString> </NewLineString> <IndentString> </IndentString>
I'm adding the values of each XELement
to a data dictionary using a LINQ query; the .ForEach
part looks like this:
.ForEach(x => SchemaDictionary.Add( LogicHelper.GetEnumValue(x.Name.ToString()), x.Value));
To test to see if the whitespace values were preserved, I'm printing out a line of the character numbers of each value item in the data dictionary. In the following code, x
represents a KeyValuePair
and the Aggregate
is simply making a string of the character integer values:
x.Value.ToCharArray() .Aggregate<char,string>("",(word,c) => word + ((int)c).ToString() + " " ) ));
I expected to see 10 13
for the <NewLineString>
value and 32 32 32 32
for the <IndentString>
value. However, nothing was printed for each value ( note: other escaped values in the XML such as <
printed their character numbers correctly ).
What do I need to do in either my C# code or my XML document so that my parser adds the complete whitespace string to the Data Dictionary?
尝试使用LoadOptions.PreserveWhitespace加载XDocument
Try loading your document this way.
XmlDocument doc = new XmlDocument();
doc.PreserveWhitespace = true;
doc.Load("book.xml");
or just modify your input xml to:
<NewLineString> </NewLineString>
<IndentString xml:space="preserve"> </IndentString>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.