Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sharp Develop not recognizing XML closing tags

Status
Not open for further replies.

brialex

Technical User
Jan 6, 2004
26
US
I am using a SharpDevelop program that takes an XML file and converts it to the format I need for data upload. The
client is send the XML file with <test/> as the closing tag when a data field is empty instead of <test></test>.
Does anyone know of a way I can change those tags to a well
fromed XML file. The program I am using doesn't seem to recognize the database fiels that have <test/> as the tag. Here's and example of how their file looks

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<lines>
<line>
<pub_code>YES</pub_code>
<customer_number/>
<rte_cde/>
<rate>17</rate>
<key_code>DYS1569X</key_code>
<bil_cde>B</bil_cde>
<donor_number/>
<strt_dte>20070101</strt_dte>
<term_offer_1>5</term_offer_1>
<amount_paid/>
<cc_info/>
<gro_rate/>
<card_to/>
<gft_msg/>
<atn_1st>Carly</atn_1st>
<atn_mid/>
<atn_end>Thomas</atn_end>
<cmp_nme/>
<str_1st>168 Clinton St</str_1st>
<str_2nd/>
<str_3rd/>
<ctm_cty>Columbus</ctm_cty>
<ctm_ste>OH</ctm_ste>
<cun_typ/>
<zip_cde>43202</zip_cde>
<pho_nbr/>
<pho_nbr2/>
<adr_email/>
<promo/>
</line>
</lines>

The program works fine is all empty fields appear lik my example below:

<customer_number></customer_number>

Thanks
Trisha
 
What program are you using? With regards to the XML not being well-formed, unfortunately for you <tag/> is well-formed XML, and your parser should recognise it as such. What error are you getting?

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
All of the fields that have <tag/>, meaning the field is empty, pop up with an error that say field name can't be found. If I change the XML to <tag></tag> the file runs through just fine.
 
I am using a SharpDevelop program...
#develop is an IDE, not a programming language. It's probably written in C#, but #develop now supports other languages.


We're probably going to need to see some of the source code in order to help you.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
This will be a bit lengthy but here is some of the source

BuildFileForm.cs

using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.ComponentModel;

namespace FFB
{
/// <summary>
/// Description of BuildFileForm.
/// </summary>
public class BuildFileForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Label _captionLabel;
private LineReader _lineReader;
private LineBuilder _lineBuilder;
private string _outputFile;
private Stream _stream;
private StreamWriter _writer;

public BuildFileForm(LineReader lineReader, LineBuilder lineBuilder, string outputFile)
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();

_lineReader = lineReader;
_lineBuilder = lineBuilder;
_outputFile = outputFile;
_stream = null;
_writer = null;

this.Closing += new CancelEventHandler(thisClosing);
this.Show();
}

#region Windows Forms Designer generated code
/// <summary>
/// This method is required for Windows Forms designer support.
/// Do not change the method contents inside the source code editor. The Forms designer might
/// not be able to load this method if it was changed manually.
/// </summary>
private void InitializeComponent() {
this._captionLabel = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// _captionLabel
//
this._captionLabel.Font = new System.Drawing.Font("Tahoma", 21.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this._captionLabel.ForeColor = System.Drawing.Color.Black;
this._captionLabel.Location = new System.Drawing.Point(48, 24);
this._captionLabel.Name = "_captionLabel";
this._captionLabel.Size = new System.Drawing.Size(176, 40);
this._captionLabel.TabIndex = 0;
this._captionLabel.Text = "Building...";
//
// BuildFileForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.Color.White;
this.ClientSize = new System.Drawing.Size(280, 85);
this.Controls.Add(this._captionLabel);
this.Name = "BuildFileForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "File Builder";
this.ResumeLayout(false);
}
#endregion

public void Build()
{
_stream = File.Open(_outputFile, FileMode.Create);
_writer = new StreamWriter(_stream);

while(_lineReader.GetNext())
{
string line = _lineBuilder.GetLine();
_writer.WriteLine(line);
}

_writer.Close();
_stream.Close();

_captionLabel.Text = "Done.";
this.Refresh();
}

public void thisClosing(object o, CancelEventArgs e)
{
_writer.Close();
_stream.Close();
}
}
}



XmlLineReader.cs

using System;
using System.Collections;
using System.Xml;


namespace FFB
{
public class XmlLineReader : LineReader
{
private XmlTextReader _reader;

public XmlLineReader()
{
_reader = null;
}

public override void GetData()
{
if(_reader != null)
_reader.Close();
_reader = new XmlTextReader(_dataFile);
}

public override bool GetNext()
{
try
{
if(!_reader.EOF)
{
_vars.Clear();

_reader.Read();

// Find start of record
while(_reader.Name.ToLower() != Globals.RECORD_START && !_reader.EOF)
_reader.Read();

if(_reader.EOF) return false;

// Add data to vars till end of record
do
{
_reader.Read();

// Read till named node found
while(_reader.Name == "" && !_reader.EOF)
_reader.Read();

if(_reader.EOF) return false;

string nodeName = _reader.Name.ToLower();

// Next node should have value
if(nodeName != Globals.RECORD_START)
{
_reader.Read();
_vars[nodeName] = _reader.Value;
_reader.Read();
}

} while(_reader.Name.ToLower() != Globals.RECORD_START && !_reader.EOF);
}
else
{
return false;
}
}
catch(Exception e)
{
Console.WriteLine("Error in XML data file " + _dataFile + " :" + e.Message);
}

return true;
}
}
}


LineReader.cs

using System.Collections;
using System;

namespace FFB
{
public class LineReader
{
internal Hashtable _vars;
internal string _dataFile;

public LineReader()
{
_vars = new Hashtable();
_dataFile = "";
}

public virtual void GetData() {}

public virtual bool GetNext() { return false; }

public Hashtable Vars
{
get
{
return _vars;
}
}

public string DataFile
{
get
{
return _dataFile;
}
set
{
_dataFile = value;
}
}

}

}
 
It seems to use a naive scheme where it is assumed that every named node is followed by a content node (even if it is an empty string). As you have established empirically, this doesn't always follow.

Once you have found the named node, you need to check its IsEmptyElement property. If it's true, then you need to
Code:
_vars[nodeName] = "NULL";
or whatever value you want to use to indicate that the tag is empty.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top