By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,526 Members | 1,895 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,526 IT Pros & Developers. It's quick & easy.

Problem writing xml with xmltextwriter

P: n/a
I am using the following code to manufactue xml using xmltextwriter. The xml
is okay except for the extra set of start document and the outer <string>
element. Where do they come from and how do I keep those from being written?

<?xml version="1.0" encoding="utf-8" ?>
- <string xmlns="http://www.Employee.com/abc/">
<?xml version="1.0" encoding="utf-8" ?>
- <Services xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.Employee.com/abc/">
- <Employee>
<LevelNames>ABC</LevelNames>
<LevelCodes>123</LevelCodes>
</Employee>
- <Employee>
<LevelNames>FRE</LevelNames>
<LevelCodes>564</LevelCodes>
</Employee>
- <Employee>
<LevelNames>FRP</LevelNames>
<LevelCodes>764</LevelCodes>
</Employee>
</Services>
</string>

public string _LevelStream()
{
Department.Users oc = new Department.Users();
Department.Level oca;
oca = oc.GetListOfLevels();
int nL = oca.LevelNames.Length;
int cL = oca.LevelCodes.Length;
string [,] strArr;
strArr = new string[nL, cL];

for (int i=0; i<nL; ++i)
{
strArr[i, 0] = oca.LevelNames[i].ToString();
}
for (int i=0; i<cL; ++i)
{
strArr[i, 1] = oca.LevelCodes[i].ToString();
}
//Build XML
System.IO.Stream m = new System.IO.MemoryStream();
XmlTextWriter xw = new XmlTextWriter(m, System.Text.Encoding.UTF8);
xw.Formatting = Formatting.Indented;
xw.WriteStartDocument();
xw.WriteStartElement("Services");
xw.WriteAttributeString("xmlns", "xsd", null,
"http://www.w3.org/2001/XMLSchema");
xw.WriteAttributeString("xmlns", "xsi", null,
"http://www.w3.org/2001/XMLSchema-instance");
xw.WriteAttributeString("xmlns", "http://www.Employee.com/abc/");

for (int b=0; b<strArr.GetLength(1); ++b)
{
xw.WriteStartElement("Level");
xw.WriteElementString("LevelNames", strArr[b,0]);
xw.WriteElementString("LevelCodes", strArr[b,1]);
xw.WriteEndElement();
}

xw.WriteEndDocument();
xw.Flush();
m.Position=1;
byte[] bt = new byte[m.Length];
m.Read(bt,0,(int)m.Length);

xw.Close();
string theString = System.Text.Encoding.UTF8.GetString(bt, 0, bt.Length);
Console.WriteLine(theString);
return theString;
}
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Mori" <Mo**@discussions.microsoft.com> wrote in message news:B1**********************************@microsof t.com...
except for the extra set of start document and the outer <string>
element. Where do they come from and how do I keep those
from being written?
When I run the example code you've supplied, I don't get the
additional elements before the start of the document. Are
you certain you're not re-using the MemoryStream or byte[]
more than once?

BTW, to get the XML you presented, one change was req'd:
for (int b=0; b<strArr.GetLength(1); ++b)
This needs to use strArr.GetLength(0) to show 3 elements,
because it's the second rank (1) that's limited to 2.

: : xw.WriteElementString("LevelNames", strArr[b,0]);
xw.WriteElementString("LevelCodes", strArr[b,1]);


As I don't have your Department.Users classes, the test
project I ran was this,

- - - MoriTest1019.cs
using System;
using System.IO;
using System.Text;
using System.Xml;

public class MoriTest1019App
{
public static void Main( )
{
// Emulate Levels of Department.User to produce example XML.
string [,] strArr;
strArr = new string[ 3, 2];
strArr[ 0, 0] = "ABC"; strArr[ 1, 0] = "FRE"; strArr[ 2, 0] = "FRP";
strArr[ 0, 1] = "123"; strArr[ 1, 1] = "564"; strArr[ 2, 1] = "764";

// Build XML.
Stream m = new System.IO.MemoryStream( );
XmlTextWriter xw = new XmlTextWriter( m, Encoding.UTF8);
xw.Formatting = Formatting.Indented;
xw.WriteStartDocument( );
xw.WriteStartElement( "Services");
xw.WriteAttributeString( "xmlns", "xsd", null,
"http://www.w3.org/2001/XMLSchema");
xw.WriteAttributeString( "xmlns", "xsi", null,
"http://www.w3.org/2001/XMLSchema-instance");
xw.WriteAttributeString("xmlns", "http://www.Employee.com/abc/");

for (int b = 0; b < strArr.GetLength(0); ++b)
{
xw.WriteStartElement( "Level");
xw.WriteElementString( "LevelNames", strArr[ b, 0]);
xw.WriteElementString( "LevelCodes", strArr[ b,1]);
xw.WriteEndElement( );
}

xw.WriteEndDocument( );
xw.Flush( );
m.Position=1;
byte[] bt = new byte[ m.Length];
m.Read( bt, 0, (int)m.Length);

xw.Close( );
string theString = Encoding.UTF8.GetString( bt, 0, bt.Length);
Console.WriteLine( theString);
}
}
- - -

There must be something you've left out. Definitely check
that m and bt don't retain old data.
Derek Harmon
Nov 21 '05 #2

P: n/a
Thanks for your Help. Your example worked, and did not display the extra
startdocument like I saw in my code. However, I am still at a loss to find
out what I am missing. I shall examine the code again.
"Derek Harmon" wrote:
"Mori" <Mo**@discussions.microsoft.com> wrote in message news:B1**********************************@microsof t.com...
except for the extra set of start document and the outer <string>
element. Where do they come from and how do I keep those
from being written?


When I run the example code you've supplied, I don't get the
additional elements before the start of the document. Are
you certain you're not re-using the MemoryStream or byte[]
more than once?

BTW, to get the XML you presented, one change was req'd:
for (int b=0; b<strArr.GetLength(1); ++b)


This needs to use strArr.GetLength(0) to show 3 elements,
because it's the second rank (1) that's limited to 2.

: :
xw.WriteElementString("LevelNames", strArr[b,0]);
xw.WriteElementString("LevelCodes", strArr[b,1]);


As I don't have your Department.Users classes, the test
project I ran was this,

- - - MoriTest1019.cs
using System;
using System.IO;
using System.Text;
using System.Xml;

public class MoriTest1019App
{
public static void Main( )
{
// Emulate Levels of Department.User to produce example XML.
string [,] strArr;
strArr = new string[ 3, 2];
strArr[ 0, 0] = "ABC"; strArr[ 1, 0] = "FRE"; strArr[ 2, 0] = "FRP";
strArr[ 0, 1] = "123"; strArr[ 1, 1] = "564"; strArr[ 2, 1] = "764";

// Build XML.
Stream m = new System.IO.MemoryStream( );
XmlTextWriter xw = new XmlTextWriter( m, Encoding.UTF8);
xw.Formatting = Formatting.Indented;
xw.WriteStartDocument( );
xw.WriteStartElement( "Services");
xw.WriteAttributeString( "xmlns", "xsd", null,
"http://www.w3.org/2001/XMLSchema");
xw.WriteAttributeString( "xmlns", "xsi", null,
"http://www.w3.org/2001/XMLSchema-instance");
xw.WriteAttributeString("xmlns", "http://www.Employee.com/abc/");

for (int b = 0; b < strArr.GetLength(0); ++b)
{
xw.WriteStartElement( "Level");
xw.WriteElementString( "LevelNames", strArr[ b, 0]);
xw.WriteElementString( "LevelCodes", strArr[ b,1]);
xw.WriteEndElement( );
}

xw.WriteEndDocument( );
xw.Flush( );
m.Position=1;
byte[] bt = new byte[ m.Length];
m.Read( bt, 0, (int)m.Length);

xw.Close( );
string theString = Encoding.UTF8.GetString( bt, 0, bt.Length);
Console.WriteLine( theString);
}
}
- - -

There must be something you've left out. Definitely check
that m and bt don't retain old data.
Derek Harmon

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.