Harper Martin Harper Martin - 7 months ago 27
C# Question

Xml Node value never change error

I got this xml from calling Zoho Crm api: https://ix-infiniti-preview.azurewebsites.net/Manage/zohotest.xml

I wanted to loop through each row of this xml and create a DataTable row data with each row. This is what I came up with:

//Get List of fields
List<string> fields = data.Columns.Cast<DataColumn>()
.Select(x => x.ColumnName)
.ToList();

List<MalformedLineException> exceptions = new List<MalformedLineException>();
List<string> values = new List<string>();

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlData);

// Count number of row returned
XmlNodeList xmlNL = xmlDoc.GetElementsByTagName("row");
int rowCount = xmlNL.Count;

for (int row = 1; row <= rowCount; row++)
{
DataRow currentRow = data.NewRow();
XmlNodeList xnList = xmlDoc.SelectNodes("/response/result/" + moduleName +"/row[@no='" + row + "']");
foreach (XmlNode xn in xnList)
{
foreach (string field in fields)
{
XmlNode objNode = xn.SelectSingleNode("//FL[@val='" + field + "']");
if (objNode != null)
{
string value = objNode.InnerText;
currentRow[field] = value;
}
}
}
data.Rows.Add(currentRow);
}
data.EndLoadData();


This piece of code works fine for row 1 and map all the values to DataColums perfectly. But when it comes to row 2 (and 18 other rows in the actual xml) all the values of these rows will be exactly the same with row 1. So basically I got 20 of row 1 displayed as a result. Not sure what is going on here please help me out with this. Thanks

Answer Source

I cant fully test this until I have your definition of fields.

        // You will need to ensure these are right
        DataTable data = new DataTable();
        List<string> fields = new List<string>()
        {
            "LEADID", "SMOWNERID", "Lead Owner", "Company", "First Name", "Last Name", "Email", "Phone", "Lead Source", "Created By", "SMCREATORID", "MODIFIEDBY", "Modified By", "Created Time", "Modified Time", "Street", "City", "State", "Zip Code", "Last Activity Time", "Lead Type", "Practice name", "SMS Opt Out", "Send SMS"
        };

        foreach (var field in fields)
        {
            DataColumn column = new DataColumn(field, typeof(String));
            data.Columns.Add(column);
        }

        string xmlText;
        using (var client = new WebClient())
        {
            IWebProxy defaultWebProxy = WebRequest.DefaultWebProxy;
            defaultWebProxy.Credentials = CredentialCache.DefaultCredentials;
            client.Proxy = defaultWebProxy;

            xmlText = client.DownloadString(@"https://ix-infiniti-preview.azurewebsites.net/Manage/zohotest.xml");
        }

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(xmlText);

        XmlNodeList xnList = xmlDoc.SelectNodes("/response/result/Leads/row");

        foreach (var row in xnList)
        {
            XmlElement item = row as XmlElement;
            Debug.WriteLine(item.Attributes["no"].Value);

            DataRow currentRow = data.NewRow();
            List<string> rowData = new List<string>();
            foreach (XmlElement node in item.ChildNodes)
            {
                if (fields.Contains(node.Attributes["val"].Value))
                {
                    Console.WriteLine(node.Attributes["val"].Value + ":" + node.InnerText);

                    currentRow[fields.IndexOf(node.Attributes["val"].Value)] = node.InnerText;

                }
            }
            data.Rows.Add(currentRow);
        }

        data.AcceptChanges();