472,353 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

Problem writing xml with xmltextwriter

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
2 3275
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Dan | last post by:
Is there a better way to include the XML declaration than the following? XmlDeclaration dec...
4
by: z. f. | last post by:
i have xml with the line <VAL ID="artist" VAL="abc & cde"/> i need the & character to be there but the xmlDocument Load method throw...
0
by: Marc Schumacher via DotNetMonster.com | last post by:
Hi, I am writing a client/server application with a desktop and a pocketPC which transfers a xml string which is to be loaded by a dataset on the...
1
by: Dan | last post by:
How would I write an XmlDocument to a file so that each node is properly indented and followed by a carriage return? Thanks...Dan
1
by: Dan | last post by:
I'm trying to write an XmlDocument to disk using an XmlTextWriter. Everything is fine except that I can't get the XML declaration to come out. I'm...
13
by: jcrouse | last post by:
Here is some of my code Dim myXmlTextWriter As XmlTextWriter = New XmlTextWriter("C:\CPViewer\CPViewer.xml", System.Text.Encoding.Unicode ...
1
by: Ray | last post by:
Dear all, I am using XmlTextWriter for writing the xml file. The code is as follow: Dim test As XmlTextWriter = Nothing Dim UTF8 As New...
3
by: GaryDean | last post by:
I'm using an XmlTextWriter and it's various methods such as WriteElementString, WriteStartElement, WriteEndElement, etc to create an xml document. ...
1
by: u8dmtm | last post by:
I want to output XML from an ASP.Net page and have created the code below: using System; using System.Data; using System.Configuration; using...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.