Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello!

I have an XML that contains various <bold> and <italic> tags.

I am loading the file with
C#
Xdocument.Load(filename, LoadOptions.PreserveWhiteSpace)
, but after removing the tags and writing it to another file, all the <bold> and <italic> tags are getting replaced by ("\n").

For example,

XML
<text><bold>Text</bold><text>
is:
XML
<text>
Text
</text>

XML
<text><bold><italic>Text</bold></italic><text>
is:
XML
<text>


Text

</text>

I want it to be:
XML
<text>Text</text>


Please help.

Regards
Aman

What I have tried:

C#
Xdocument.Load(filename, LoadOptions.PreserveWhiteSpace)
Posted
Updated 11-Feb-19 8:14am

That's what LoadOptions.PreserveWhitespace[^] does - it preserves all the insignificant whitespace characters in the input file. And whitespace is ' ' at the beginning or end, or more than one ' ' in a row; newlines '\n'; and tabs '\t'

Remove the option, and it'll probably disappear. If it doesn't, you need to look very closely at your input file.
 
Share this answer
 
Comments
Primo Chalice 11-Feb-19 3:47am    
Hello!

What happens is that when I don't give that option, then tags like <bold>Bold <italic>Italic are concatenated and I need those spaces between them.

Without LoadOptions - BoldItalic
With LoadOptions - Bold Italic -- This is what I want.

My main XML has a structure like <paragraph><bold><italic>Text Goes Here< /italic>< /bold>< /paragraph>. This becomes:

<paragraph>
<bold>
<italic>Text Goes Here
< /italic> -- ignore the space
< /bold> -- ignore the space
< /paragraph> -- ignore the space

I think this is the problem. Is there a way to prevent this from happening?

I am getting one result correctly and the other is at fault.

Please help.

Regards
Aman
OriginalGriff 11-Feb-19 3:52am    
Have a look here: https://www.tutorialspoint.com/xml/xml_white_spaces.htm
Primo Chalice 11-Feb-19 4:32am    
Hello!

Is there a way of preventing the child nodes from going to the next line i.e. preserving the main XML structure?

Regards
Aman
OriginalGriff 11-Feb-19 4:52am    
That doesn;t make a lot of sense in isolation - bear in mind we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Primo Chalice 11-Feb-19 4:56am    
Yes, sorry. What I meant was that since I am using XDocument.Load(), I think that it is restructuring the XML file and sending the child nodes below their respective parent nodes. So, i just wanted to know that is there a way to load the XML file using XDocument.Load() but without changing the original file?
When you specify LoadOptions.PreserveWhiteSpace while loading there will be no whitespace added or removed, so I do not think this is where the problem is.

I think your problem is on saving.

Take a look at the SaveOptions Enum (System.Xml.Linq) | Microsoft Docs[^].

It is an optional parameter on XDocument.Save.
Specifically you would need the DisableFormatting flag to ensure the writer doesn't insert insignificant whitespaces.

But in all cases, you need to learn to debug. Do not try to just look at the input and output and then randomly tweak some code. Single step over the relevant code in the debugger and observe it.

Are the extra newlines present after load? If yes, you need to look into how to load it correctly.

Are they present after removing the bold tags etc? If so, you need to look into the code doing the replacement and try to come up with a solution.

If they are not present in the element when you call save, then it is added by save and you need to look into the flags you pass on to save.

Be careful with the debugger in Visual Studio, it tries to "help" you by rendering new lines as spaces sometimes. Use the "text visualizer" available by clicking the small dropdown menu shown with a magnifying glass next to the value.

Most likely using this flag is the correct approach in your case. But if you are responsible for generating the XML files, and you want to minimize other tools making similar errors processing your files "down the line", you should look into the xml:space attribute. It tells any standard compliant XML writer/loaded to preserve the significant whitespaces on both load and save without any additional parameters being needed. Specify it at the root element if you are lazy (and want to make it less likely you forget it somewhere), or to individual elements if you want to keep it "nice" where it can still format as much as possible.
 
Share this answer
 
v2
Quote:
is:
XML
<text>


Text

</text>


I want it to be:
XML
<text>Text</text>


Your xml structure is exactly what it is, because...
Quote:
after removing the tags and writing it to another file, all the <bold> and <italic> tags are getting replaced by ("\n").


Conclusion: replace unnecessary tags with empty string.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900