Bertrand Caillet

Hi,

I have a wierd issue in my C# code. When I update a field in an XML file with a empty string, the field is actually updated with "\r\n "

I try several things like String.Empty but nothing is working correctly.

Does anybody have a clue how to resolve this

Thanks,

Bertrand




Re: Visual C# General Wrong Empty string convertion

TaylorMichaelL

That would be the newline character. String.Empty won't work with that. You could try calling Trim to strip out all the extra stuff. Otherwise post the code you are using to manipulate and write out the string and perhaps we can see what is wrong with it.

Michael Taylor - 1/22/07
http://p3net.mvps.org





Re: Visual C# General Wrong Empty string convertion

Bertrand Caillet

Hi Michael,

I actually believe that the core problem is when the Xml file is saved.

My code is

XmlNodeList elemList17 = doc.GetElementsByTagName("Machine");
elemList17[0].InnerText = (
String.IsNullOrEmpty(this.SEMachineID.Text) string .Empty : this.SEMachineID .Text);
/// there the text is OK ""

doc.Save(filename);

after the saving, it is now "\r\n ".

Why






Re: Visual C# General Wrong Empty string convertion

Ross Dargan

try

XmlNodeList elemList17 = doc.GetElementsByTagName("Machine");
elemList17[0].InnerText = (
String.IsNullOrEmpty(this.SEMachineID.Text) "" : this.SEMachineID .Text);
/// there the text is OK ""

doc.Save(filename);

Might work:)

Ross






Re: Visual C# General Wrong Empty string convertion

TaylorMichaelL

Are you seeing this in your XML file and don't want it

<child>
</child>

If so this is caused by the fact that you have assigned a value to InnerText. This property represents the entire collection of child nodes of the element. When you set this property (even to an empty string) the HasChildNodes property becomes true. If this property is true then when the node is written out it'll be created with start and end elements on separate lines.

You can use the InnerXml to set the inner text (in XML format). This property does not set HasChildNodes and therefore will leave the element on one line. Alternatively you can switch to a formal Xml writer that can be configured.

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent =
false;
settings.NewLineHandling =
NewLineHandling.None;
settings.NewLineOnAttributes =
false;
using (XmlWriter writer = XmlWriter.Create(@"C:\temp\test.xml", settings))
{
doc.Save(writer);
};

This puts the entire XML file on one line (not pretty).

Michael Taylor - 1/23/07
http://p3net.mvps.org





Re: Visual C# General Wrong Empty string convertion

Bertrand Caillet

Thanks for the feedback, I really appreciate it.

I try to change the InnerText method with the InnerXml method, but I still have the newline.

I don't want to put all the entire XML file on one line as you suggested. I just want to avoid the creation of the new line if the value I fetch is empty. It should be possible. I will try to search for NewLine property in my XML document.

Any feedback will still be appreciated.

Thanks,

B






Re: Visual C# General Wrong Empty string convertion

houtexwebdev

The problem with working with XML is the fact that is IS text....we all know that.

XML Documents can also be managed through an object model.

However, what you're doing is a hybrid of the two since you're using the Object Model to modify the string contents (InnerXml treats the document as a string, not a set of nodes).

Try this perspective... There's a bunch of nodes. One of those nodes is an element of interest. Once you get that node, you want to set its value to something.

doc.SelectSingleNode("/xpath/expression/to").value=string.Empty.

that would explicitly set the value of that element to a zero-length string.

If you want to make sure that that node doesn't have child nodes (elements, attributes, etc)

doc.SelectSingleNode("/xpath/expression/to").RemoveAll();

If you're handling the document through the object model, make sure you always manage its structure as a series of nodes and values, not as a string of text. That should clear a lot of things up for ya.





Re: Visual C# General Wrong Empty string convertion

Bertrand Caillet

Hi,

not sure I understand your last comment. But yes, all my values are embedded in nodes.

I try the following:

XmlNodeList elemList17 = doc.GetElementsByTagName("Machine");

elemList17[0].InnerText = ( String.IsNullOrEmpty(this.SEMachineID.Text) string.Empty : this.SEMachineID .Text);

elemList17[0].RemoveAll();

But it is still not working:

<Machine>
</Machine>

The work around is to insert a space and change the logical inside my C# tool but any idea is always welcomed!

Thanks