Why is the second element in this XML not being picked up?

  • Thread starter Thread starter chance
  • Start date Start date
C

chance

my code:

//read XML and append
XmlReader rdr = XmlReader.Create("parms.xml");
while (rdr.Read())
{
//get name and value to prepare clause
if (rdr.NodeType == XmlNodeType.Element)
{
switch(rdr.Name)
{
case "col":
whereClause += " AND " +
rdr.ReadElementContentAsString() + "=";
break;
case "val":
whereClause +=
rdr.ReadElementContentAsString();
break;
}
}
}

xml input:
<?xml version="1.0" encoding="utf-8"?>
<parameters>
<parm>
<col>
notice_of_violation.notice_of_violation_id
</col>
<val>
A1000125
</val>
</parm>
</parameters>


I'm puzzled. Any insight appreciated.

tia,
chance
 
chance said:
my code:

//read XML and append
XmlReader rdr = XmlReader.Create("parms.xml");
while (rdr.Read())
{
//get name and value to prepare clause
if (rdr.NodeType == XmlNodeType.Element)
{
switch(rdr.Name)
{
case "col":
whereClause += " AND " +
rdr.ReadElementContentAsString() + "=";
break;
case "val":
whereClause +=
rdr.ReadElementContentAsString();
break;
}
}
}

xml input:
<?xml version="1.0" encoding="utf-8"?>
<parameters>
<parm>
<col>
notice_of_violation.notice_of_violation_id
</col>
<val>
A1000125
</val>
</parm>
</parameters>


I'm puzzled. Any insight appreciated.

Can you be more specific on what the problem is? You have four elements in
your XML, the second being <parm>. A quick test shows that element is read
fine using the code above. In fact, all four elements are read fine.
Please provide additional detail on the behavior you are seeing, and your
expected results.
 
What function are you using to read the data?

When I use ReadElementContentAsString it is giving me back escaped
newlines which is screwing up my SQL clause....

Can someone suggest another?
 
chance said:
When I use ReadElementContentAsString it is giving me back escaped
newlines which is screwing up my SQL clause....

The XmlReader methods do not escape strings, if they do anything then
they unescape XML escape mechanisms like character references (e.g.

to the character). Are you looking at the string in the Visual
Studio debugger? There you might get it displayed with e.g. \n but that
is just the way the debugger chooses to display the string.
 
chance said:
What function are you using to read the data?

When I use ReadElementContentAsString it is giving me back escaped
newlines which is screwing up my SQL clause....

Can someone suggest another?

I copied and pasted your code. As you didn't say what the exact problem was
and instead stated only that the second element was not being picked up, it
was unclear what you were trying to achieve. To have the returned string
not include whitespace characters, simply trim the string. Ex:

whereClause += " AND " + rdr.ReadElementContentAsString().Trim() + "=";
 
Chance,

Is there some specific reason why you aren't doing something like this:
private void SerializeData_Click(object sender, EventArgs e)
{
XmlSerializer xs = new XmlSerializer(typeof(MyXmlData));

using (StreamWriter sw = new StreamWriter("Test.xml", false))
{
MyXmlData test = new MyXmlData();
test.Data.Add(new MyParm("column1", "value1"));
test.Data.Add(new MyParm("column2", "value2"));
test.Data.Add(new MyParm("column3", "value3"));
test.Data.Add(new MyParm("column4", "value4"));

xs.Serialize(sw, test);
}
}


private void DeserializeData_Click(object sender, EventArgs e)
{
XmlSerializer xs = new XmlSerializer(typeof(MyXmlData));

using (StreamReader sr = new StreamReader("Test.xml"))
{
MyXmlData test = xs.Deserialize(sr) as MyXmlData;

foreach(MyParm parm in test.Data)
{
MessageBox.Show(this, string.Format(
"Column {0} Value {1}", parm.Column, parm.Value));
}
}
}

Your datat object would look like this:
[XmlRoot("parameters", Namespace = "MyNameSpace")]
public class MyXmlData
{
[XmlElement("parm")]
public List<MyParm> Data = new List<MyParm>();
}

public class MyParm
{
private string _column;
private string _value;

public MyParm()
{
_column = string.Empty;
_value = string.Empty;
}

public MyParm(string column, string value)
{
Column = column;
Value = value;
}

[XmlElement("col")]
public string Column
{
get { return _column; }
set { _column = value; }
}

[XmlElement("val")]
public string Value
{
get { return _value; }
set { _value = value; }
}
}



Where you xml data would look like this:
<?xml version="1.0" encoding="utf-8"?>
<parameters xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="MyNameSpace">
<parm>
<col>column1</col>
<val>value1</val>
</parm>
<parm>
<col>column2</col>
<val>value2</val>
</parm>
<parm>
<col>column3</col>
<val>value3</val>
</parm>
<parm>
<col>column4</col>
<val>value4</val>
</parm>
</parameters>


Dave
 
Back
Top