hi,
Looking at the referenced document:
<test>•
••••<item>•
••••••••<item xml:space="preserve">º
ºººººººººººº<item/>º
ºººººººº</item>•
••••</item>•
••••<book>º
ºººººººº<b>This<b>º
ºººººººº<i>is</i>º
ºººººººº<b>a test</b>º
ºººº</book>•
</test>•
The white space shown as (•) is insignificant white space. The white
space shown as (º) is significant white space.
Note The scope of the xml:space attribute changes what would normally
be considered insignificant white space to be significant white space.
Notice that <b>a test</b> is not shown to be affected by the space=preserve
command.
I did try adding this to my xml (two ways), but it had no effect on the
xmltextreader.
<DBRow xml:space="preserve">
<DBColumn />
<DBColumn xml:space="preserve"> </DBColumn>
</DBRow>
<DBRow xml:space="preserve">
<DBColumn />
<DBColumn> </DBColumn>
</DBRow>
Let try this from a different direction:
- I am not using validating readers, xsl, dom, etc.
- I want to output text data and read it back in, and the data can contain
spaces or even conceivably be all spaces
- How do I make sure a space out becomes a space in?
- Note: it seems that having nonspace characters causes space characters to
be read in. Is this always true?
Thanks
[quoted text, click to view] "Kevin Yu [MSFT]" wrote:
> Hi Ken,
>
> I'm afraid this is by design for the XmlTextReader. If we don't have any
> context within an element the whitespaces are abandoned if we don't have
> xml:space="preserve" attribute set in the element. Please check the MSDN
> document I posted in my last post for more information. Thanks!
>
> Kevin Yu
> =======
> "This posting is provided "AS IS" with no warranties, and confers no
> rights."
>
In what manner are they preserved for you?
I am looking to get a XmlNodeType.Text returned with the elements text
data. Is that what you get? or are you getting whitespace returned?
Here are two data snippet examples I have tried:
<DataBase>
<Index>1</Index>
<Enabled>True</Enabled>
<Series>2</Series>
<DBValues>
<dummy />
<DBRow xml:space="preserve">
<DBColumn />
<DBColumn>1</DBColumn>
</DBRow>
<DBRow xml:space="preserve">
<DBColumn />
<DBColumn>1</DBColumn>
</DBRow>
<DBRow xml:space="preserve">
<DBColumn />
<DBColumn xml:space="preserve"> </DBColumn>
</DBRow>
<DBRow xml:space="preserve">
<DBColumn />
<DBColumn />
</DBRow>
</DBValues>
</DataBase>
<DataBase>
<Index>1</Index>
<Enabled>True</Enabled>
<Series>2</Series>
<DBValues>
<dummy />
<DBRow>
<DBColumn />
<DBColumn>1.1</DBColumn>
<DBColumn>-2</DBColumn>
<DBColumn>3</DBColumn>
<DBColumn>rrr</DBColumn>
</DBRow>
<DBRow>
<DBColumn />
<DBColumn>-1.1</DBColumn>
<DBColumn>2.0</DBColumn>
<DBColumn>-3</DBColumn>
<DBColumn>sss</DBColumn>
</DBRow>
<DBRow>
<DBColumn />
<DBColumn>10</DBColumn>
<DBColumn> </DBColumn>
<DBColumn>1</DBColumn>
<DBColumn>ttt</DBColumn>
</DBRow>
<DBRow>
<DBColumn />
<DBColumn>one</DBColumn>
<DBColumn>two</DBColumn>
<DBColumn>three</DBColumn>
<DBColumn>vvv</DBColumn>
</DBRow>
<DBRow>
<DBColumn />
<DBColumn>11</DBColumn>
<DBColumn> </DBColumn>
<DBColumn />
<DBColumn />
</DBRow>
</DBValues>
</DataBase>
//here is a sample code snippet,
//all the storing of data and error checking is gone,
//as well as getting data from a string instead of a file
using System;
using System.Collections;
using System.Xml;
namespace ConsoleApplication1
{
public class MyClass
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
GetData();
RL();
}
private static void RL()
{
Console.ReadLine();
}
private static void GetData()
{
string xmlFrag = " <DataBase>\n"+
" <Index>1</Index>\n"+
" <Enabled>True</Enabled>\n"+
" <Series>2</Series>\n"+
" <DBValues>\n"+
" <dummy />\n"+
" <DBRow>\n"+
" <DBColumn />\n"+
" <DBColumn>1.1</DBColumn>\n"+
" <DBColumn> </DBColumn>\n"+
" <DBColumn>3</DBColumn>\n"+
" <DBColumn>rrr</DBColumn>\n"+
" </DBRow>\n"+
" <DBRow>\n"+
" <DBColumn />\n"+
" <DBColumn>-1.1</DBColumn>\n"+
" <DBColumn>2.0</DBColumn>\n"+
" <DBColumn>-3</DBColumn>\n"+
" <DBColumn>sss</DBColumn>\n"+
" </DBRow>\n"+
" <DBRow>\n"+
" <DBColumn />\n"+
" <DBColumn>10</DBColumn>\n"+
" <DBColumn> </DBColumn>\n"+
" <DBColumn>1</DBColumn>\n"+
" <DBColumn>ttt</DBColumn>\n"+
" </DBRow>\n"+
" <DBRow>\n"+
" <DBColumn />\n"+
" <DBColumn>one</DBColumn>\n"+
" <DBColumn>two</DBColumn>\n"+
" <DBColumn>three</DBColumn>\n"+
" <DBColumn>vvv</DBColumn>\n"+
" </DBRow>\n"+
" <DBRow>\n"+
" <DBColumn />\n"+
" <DBColumn>11</DBColumn>\n"+
" <DBColumn> </DBColumn>\n"+
" <DBColumn />\n"+
" <DBColumn />\n"+
" </DBRow>\n"+
" </DBValues>\n"+
" </DataBase>\n"
;
//Create the XmlNamespaceManager.
NameTable nt = new NameTable();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
nsmgr.AddNamespace("bk", "urn:sample");
//Create the XmlParserContext.
XmlParserContext context = new XmlParserContext(null, nsmgr,
null, XmlSpace.None);
System.Xml.XmlTextReader xr = new
System.Xml.XmlTextReader(xmlFrag, XmlNodeType.Element, context);
xr.WhitespaceHandling = WhitespaceHandling.All;
try
{
while (xr.Read())
{
switch(xr.NodeType)
{
case XmlNodeType.Element:
{
switch(xr.Name)
{
case "DBValues":
_ParseDBValues(xr,1);
break;
}
}
break;
}
}
}
catch
{
}
}
public static void _ParseDBValues(System.Xml.XmlTextReader xr, int
_nDBInst)
{
try
{
while (xr.Read())
{
switch(xr.NodeType)
{
case XmlNodeType.Element:
{
switch(xr.Name)
{
case "DBRow":
Console.WriteLine("ROW");
_ParseDBColObject(xr, _nDBInst);
break;
}
}
break;
case XmlNodeType.Text:
break;
case XmlNodeType.EndElement:
When you say the whitespace is preserved, how exactly do you mean that?
Is the data coming in as XmlNodeType.Text or as whitespace?
// the data was written with code like the following
xw.WriteStartElement("DBValues");
xw.WriteStartElement("dummy");
xw.WriteEndElement();
xw.WriteStartElement("DBRow");
string _strColVal = something;
xw.WriteElementString("DBColumn", _strColVal);
xw.WriteEndElement();
xw.WriteEndElement();
xw.WriteEndElement();
// sample code snippet,
// all the data storing and error checking is gone
// and we are getting data from a string instead of a file
using System;
using System.Collections;
using System.Xml;
namespace ConsoleApplication1
{
public class MyClass
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
GetData();
RL();
}
private static void RL()
{
Console.ReadLine();
}
private static void GetData()
{
string xmlFrag = " <DataBase>\n"+
" <Index>1</Index>\n"+
" <Enabled>True</Enabled>\n"+
" <Series>2</Series>\n"+
" <DBValues>\n"+
" <dummy />\n"+
" <DBRow>\n"+
" <DBColumn />\n"+
" <DBColumn>1.1</DBColumn>\n"+
" <DBColumn> </DBColumn>\n"+
" <DBColumn>3</DBColumn>\n"+
" <DBColumn>rrr</DBColumn>\n"+
" </DBRow>\n"+
" <DBRow>\n"+
" <DBColumn />\n"+
" <DBColumn>-1.1</DBColumn>\n"+
" <DBColumn>2.0</DBColumn>\n"+
" <DBColumn>-3</DBColumn>\n"+
" <DBColumn>sss</DBColumn>\n"+
" </DBRow>\n"+
" <DBRow>\n"+
" <DBColumn />\n"+
" <DBColumn>10</DBColumn>\n"+
" <DBColumn> </DBColumn>\n"+
" <DBColumn>1</DBColumn>\n"+
" <DBColumn>ttt</DBColumn>\n"+
" </DBRow>\n"+
" <DBRow>\n"+
" <DBColumn />\n"+
" <DBColumn>one</DBColumn>\n"+
" <DBColumn>two</DBColumn>\n"+
" <DBColumn>three</DBColumn>\n"+
" <DBColumn>vvv</DBColumn>\n"+
" </DBRow>\n"+
" <DBRow>\n"+
" <DBColumn />\n"+
" <DBColumn>11</DBColumn>\n"+
" <DBColumn> </DBColumn>\n"+
" <DBColumn />\n"+
" <DBColumn />\n"+
" </DBRow>\n"+
" </DBValues>\n"+
" </DataBase>\n"
;
//Create the XmlNamespaceManager.
NameTable nt = new NameTable();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
nsmgr.AddNamespace("bk", "urn:sample");
//Create the XmlParserContext.
XmlParserContext context = new XmlParserContext(null, nsmgr,
null, XmlSpace.None);
System.Xml.XmlTextReader xr = new
System.Xml.XmlTextReader(xmlFrag, XmlNodeType.Element, context);
xr.WhitespaceHandling = WhitespaceHandling.All;
try
{
while (xr.Read())
{
switch(xr.NodeType)
{
case XmlNodeType.Element:
{
switch(xr.Name)
{
case "DBValues":
_ParseDBValues(xr,1);
break;
}
}
break;
}
}
}
catch
{
}
}
public static void _ParseDBValues(System.Xml.XmlTextReader xr, int
_nDBInst)
{
try
{
while (xr.Read())
{
switch(xr.NodeType)
{
case XmlNodeType.Element:
{
switch(xr.Name)
{
case "DBRow":
Console.WriteLine("ROW");
_ParseDBColObject(xr, _nDBInst);
break;
}
}
break;
case XmlNodeType.Text:
break;
case XmlNodeType.EndElement:
if(xr.Name.Equals("DBValues"))
{
return;
}
break;
default:
break;
}
}
}
catch
{
throw new Exception("Unexpected element in DBValues...");
}
}
public static void _ParseDBColObject(System.Xml.XmlTextReader xr,
int _nDBInst)
{
// Get data values & range check
string element = "";
int _nColIndex = -1;
// Parse input stream
try
{
while (xr.Read())
{
switch(xr.NodeType)
{
case XmlNodeType.Element:
{
element = xr.Name;
if(element.Equals("DBColumn"))
{
_nColIndex++; // track which column we are on
}
}
break;
case XmlNodeType.Text:
{
switch(element)
{
case "DBColumn":