XmlTextWriter: Corrupt content

G

Guest

Hello,

I am using System.Xml.XmlTextWriter in my application in order to
store some application settings to an xml file.

The code goes like this

------------------------------------------------------

public void Write(string a_sConfigFilePath)
{
XmlTextWriter xmlWriter = null;
try
{
xmlWriter = new XmlTextWriter
(a_sConfigFilePath,System.Text.Encoding.UTF8);
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.Indentation = 4;
xmlWriter.Namespaces = true;

xmlWriter.WriteStartDocument(true);
xmlWriter.WriteStartElement(m_sNamespacePrefix, ROOT_ELEMENT,
m_sNamespace);
xmlWriter.WriteStartElement(m_sNamespacePrefix, APPLICATION_ELEMENT,
m_sNamespace);
xmlWriter.WriteAttributeString("Name", m_sNamespace, m_sApplication);
xmlWriter.WriteAttributeString("Executable", m_sNamespace,
m_sExecutable);

//Write the root level setting. Subsettings will be written
recursively
_WriteSetting(m_oRootSetting, xmlWriter);

xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
xmlWriter.Flush();

}

finally
{
if (xmlWriter != null)
xmlWriter.Close();
}

---------------------------------------


In some cases I end up with corrupt content such as this:

---------------------------------------

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<ac:AppConfiguration
xmlns:ac="http://www.logicdis.gr/Software/ApplicationConfiguration">
<ac:Application ac:Name="Foo" ac:Executable=".\Foo.exe">
<ac:Setting ac:Name="SalesMenInboxForm.lsvMessageHeader"
ac:Datatype="System.String" ac:Assembly="mscorlib" ac:EditMode="None"
ac:Encrypt="False" ac:DisplayStyle="FromDatatype" ac:Descr=""
ac:Documentation="">
<ac:Setting ac:Name="Column0" ac:Datatype="System.Int32" ac:As" />
</ac:Application>
</ac:AppConfiguration>

-------------------

Note the the closing element of the first node is missing and that the inner
"Setting" node is corrupt (the attribute ac:As" should be ac:Assembly="..."
/>).

File system data corruption has crossed my mind (the file is stored on a
flash memory card) but the funny thing is that the file is not terminated
abruptly!
Only an inner portion of the document is missing!

Has anyonce come accross this?

Thanks for reading this.
 
S

Sergey Bogdanov

The problem is in the _WriteSetting(...) function. The code, you've
pasted, seems correct (AppConfiguration, Application are closed), but we
have no information about _WriteSettings.


Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com
 
G

Guest

Sorry Sergey, I ommited this code
because I think it's pretty staightforward.

Here it goes:

--------------------------

private void _WriteSetting(ConfigurationSetting a_oSetting, XmlTextWriter
a_xmlWriter)
{
bool bElementWritten = false;

//Write the configuration setting element
a_xmlWriter.WriteStartElement(m_sNamespacePrefix, SETTING_ELEMENT,
m_sNamespace);
bElementWritten = true;

//Write the setting's attributes
a_xmlWriter.WriteAttributeString(m_sNamespacePrefix,
ConfigurationManager.SETTING_NAME_ATTR, m_sNamespace,
a_oSetting.Name);

if (a_oSetting.Datatype != null)
a_xmlWriter.WriteAttributeString(m_sNamespacePrefix,
ConfigurationManager.SETTING_DATATYPE_ATTR, m_sNamespace,
a_oSetting.Datatype.FullName);

if (a_oSetting.Assembly != string.Empty && a_oSetting.Assembly != null)
a_xmlWriter.WriteAttributeString(m_sNamespacePrefix,
ConfigurationManager.SETTING_ASSEMBLY_ATTR, m_sNamespace,
a_oSetting.Assembly);

if (a_oSetting.EditMode != EditModeEnum.None)
a_xmlWriter.WriteAttributeString(m_sNamespacePrefix,
ConfigurationManager.SETTING_EDITMODE_ATTR, m_sNamespace,
a_oSetting.EditMode.ToString());

a_xmlWriter.WriteAttributeString(m_sNamespacePrefix,
ConfigurationManager.SETTING_ENCRYPTION_ATTR, m_sNamespace,
a_oSetting.Encrypt.ToString());

a_xmlWriter.WriteAttributeString(m_sNamespacePrefix,
ConfigurationManager.SETTING_DISPLAYSTYLE_ATTR, m_sNamespace,
a_oSetting.DisplayStyle.ToString());

if (a_oSetting.Description != string.Empty && a_oSetting.Description !=
null)
a_xmlWriter.WriteAttributeString(m_sNamespacePrefix,
ConfigurationManager.SETTING_DESCRIPTION_ATTR, m_sNamespace,
a_oSetting.Description);

if (a_oSetting.Documentation != string.Empty && a_oSetting.Documentation
!=
null)
a_xmlWriter.WriteAttributeString(m_sNamespacePrefix,
ConfigurationManager.SETTING_DOCUMENTATION_ATTR, m_sNamespace,
a_oSetting.Documentation);

//Write the setting's value
if (a_oSetting.Value != null)
{
string sValue = a_oSetting.Value.ToString();
if (a_oSetting.Encrypt)
sValue = _EncryptValue(sValue);
a_xmlWriter.WriteAttributeString(m_sNamespacePrefix,
ConfigurationManager.SETTING_VALUE_ATTR, m_sNamespace, sValue);
}

if (bElementWritten)
{
//Call myself recursively for each subsetting
foreach (ConfigurationSetting oSubSetting in a_oSetting)
_WriteSetting(oSubSetting, a_xmlWriter);

a_xmlWriter.WriteEndElement();
}
}

------------------------------
 
S

Sergey Bogdanov

Hm, have been trying to reproduce your error but it seems that
everything is ok, below is my small example that works fine. I suggest
you to try to identify where is a bug by writting a small application
based on your code.

private int counter = 0;
public void TestCycle(XmlTextWriter xmlWriter)
{
xmlWriter.WriteStartElement("element");
xmlWriter.WriteAttributeString("Name", "test");
xmlWriter.WriteAttributeString("Executable", "test");

if (++counter < 10) TestCycle(xmlWriter);

xmlWriter.WriteEndElement();

}

public void Test()
{
XmlTextWriter xmlWriter = null;
try
{
xmlWriter = new XmlTextWriter("/test.xml", System.Text.Encoding.UTF8);
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.Indentation = 4;

xmlWriter.WriteStartDocument(true);
xmlWriter.WriteStartElement("root");
xmlWriter.WriteStartElement("element");
xmlWriter.WriteAttributeString("Name", "test");
xmlWriter.WriteAttributeString("Executable", "test");

TestCycle(xmlWriter);


xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
xmlWriter.Flush();
}

finally
{
if (xmlWriter != null)
xmlWriter.Close();
}
}

Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com
 
G

Guest

Thank you so much for your time Sergey.
I really appreciate it!

I have't been able to reproduce the problem myself.
Perhaps flushing the writer more often
(eg. everytime an element is written)
will make things better.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top