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;
}