quick help please... XML attributes  
Author Message
CSharpShooter





PostPosted: Visual C# General, quick help please... XML attributes Top

Hi all,

I have a DataGridView that displays an XML file in it... the table has these column names: Name, Value, Data. After I've populated it I need to compare (via a method call) the data in the Name cell with the string value in the XML comment. So basically I'm comparing the Name cells value with the comment value in the XML file.
The XML file contains the useual <name>, <value> & <comment> tags.

How would I do that Also I want to color the cells Name & Value if they do not match

private void SeeForMatch()
{
XmlDocument doc = GetXmlDocument(); // loads XML file
DataTable dt = datagridView1.DataSource as DataTable;

foreach (DataRow rows in dt.Rows) // loops through the datagridview1 or should I use:
// foreach (DataGridViewRow rows in dt.Rows) 'cos I need the cell coloring
{
string name = (string)rows["name"];

foreach (XmlNode node in doc.SelectNodes("//data")) // loops through the XML file
{
if (node.Attributes["comment"].Value.Equals(name)) // !!!
// should be if ( the string value in the cell not equal to the <comment> value in the xml file)
// { color both cells & check the rest of the name column with the XML <comments> }

I know if I wanted to color the cells I would use:
rows["name"].Style.BackColor = System.Drawing.Color.Blue; //assuming rows was a DataGridView but it's DataRow type!!

Thanks for your help ppl.



Visual C#19  
 
 
TaylorMichaelL





PostPosted: Visual C# General, quick help please... XML attributes Top

Because you didn't provide the XML schema I can only guess that the actual values in your XML file are attributes of the form:

<data comment="" />

Assuming this structure then you need to a string comparison. As of v2 the best way to compare strings is to use String.Compare with options. Note that you didn't specify whether case mattered or not so I'll assume that it does.

foreach(...)
{
if (String.Compare(node.Attributes["comment"].Value, name, StringComparison.CurrentCulture) != 0)
{
//Don't match
};
}

If globalization or culture isn't an issue then you can speed things up by using StringComparison.InvariantCulture. For comparisons against file paths, keys, etc then you can do even better by using StringComparison.Ordinal. In all cases if case doesn't matter then there are additional values to ignore case. Refer to http://msdn2.microsoft.com/en-us/library/x15ca6w0.aspx for more information on string comparisons.

Michael Taylor - 11/27/06


 
 
CSharper85





PostPosted: Visual C# General, quick help please... XML attributes Top

Sorry the XML schema would be like this:

<data name="Address" xml:space="preserve">
<value>Address</value>
<comment>Address</comment>
</data>

would the above code still work Thanks.


 
 
TaylorMichaelL





PostPosted: Visual C# General, quick help please... XML attributes Top

The comment is not an attribut then but an XML element. In that case you can subselect from the parent node.

foreach(...)
{
XmlNode comment = node.SelectSingleNode("comment");
string strComment = (comment != null) comment.InnerText : "";
if (String.Compare(strComment, name, StringComparison.CurrentCulture) != 0)
{
//Don't match
};
}

Of course if you wanted the name attribute then the initial code I posted would work.

Michael Taylor - 11/27/06


 
 
CSharper85





PostPosted: Visual C# General, quick help please... XML attributes Top

How would I change the cell color if I'm looping through using DataRow
:
if (String.Compare(strComment, name, StringComparison.CurrentCulture) != 0)
{
row.Cells["name"].Style.BackColor = System.Drawing.Color.Blue; // not do able with row of type DataRow!
// I want to fill the cells that are not equal (name & value in the DataGridView)

};

With
DataGridViewRow:
foreach
(DataGridViewRow row in dt.Rows)
{
string name = row.Cells["name"].ToString();

foreach (XmlNode node in doc.SelectNodes("//data"))
{
XmlNode comment = node.SelectSingleNode("comment");
string strComment = (comment != null) comment.InnerText : "";

if (String.Compare(strComment, name, StringComparison.CurrentCultureIgnoreCase) != 0)
{
row.Cells["name"].Style.BackColor = System.Drawing.Color.DarkSalmon;
};
}
}

Thanks.


 
 
TaylorMichaelL





PostPosted: Visual C# General, quick help please... XML attributes Top

The datatable rows are of type DataRow, not DataGridViewRow. You need to enumerate the rows of the grid. For each row of the grid get the corresponding data row. Then do the comparison.

foreach(DataGridViewRow gridRow in datagridView1.Rows)
{
DataRow row = gridRow.DataBoundItem as DataRow; //Not sure that it is a row

//Do the comparison
if (no match)
gridRow.Cells["name"].Style.BackColor = Color.DarkSalmon;
};

Now the problem is that you'll enumerate the nodes of the XML file for each row in your grid which will be really slow. You should really consider a better mechanism for finding matches. If at all possible you should try to match each row in your data table to the same element in the XML document (in order). If you can't guarantee this order then you should revert to an XPath query that allows you to find the node directly. I'm not an XPath expert but I know you can find attributes/elements with specific values. You should post to the XML forums if you don't know how to do it as they will be able to help you better.

Michael Taylor - 11/27/06


 
 
CSharper85





PostPosted: Visual C# General, quick help please... XML attributes Top

Thanks Michael ... will look into it.