473,385 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 3365
"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 =m_XMLDocument.CreateXmlDeclaration("1.0",string.Empty, "yes");...
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 exception for invalid character. i don't want to encode...
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 client. the server uses this code to...
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 using the code below. What am I doing wrong? ...
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 myXmlTextWriter.Formatting =...
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 UTF8Encoding test = New XmlTextWriter("C:\abcd.xml",...
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. When I instantiate the XmlTextWriter to a file......
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 System.Collections.Generic; using System.Linq;...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.