Is this a normal xml format?

1.8k views Asked by At
<?xml version=\"1.0\" encoding=\"UTF-8\" ?> 
 <response uri=\"/crm/private/xml/Potentials/updateRecords\">
   <result>
      <message>***TEST***Record(s) updated successfully</message>
      <recorddetail>
          <FL val=\"Id\">414100000000000000</FL>
          <FL val=\"Created Time\">2012-03-30 16:54:03</FL>
          <FL val=\"Modified Time\">2012-04-11 16:53:48</FL>
          <FL val=\"Created By\"><![CDATA[Bayer]]></FL>
          <FL val=\"Modified By\"><![CDATA[Bayer]]></FL>
      </recorddetail>
  </result>
</response>

This is a generic response from Zoho stating that my record was updated successfully (minus the test part and the bogus ZohoID.) I have been trying to get my C# program to be able to make that style of xml document because when i post things to zoho it actually is very similar in style. But i just can't figure out how to make it work This is an example from their website.

<Leads>
<row no="1">
<FL val="Lead Source">Web Download</FL>
<FL val="First Name">contacto 1</FL>
<FL val="Last Name">apellido</FL>
<FL val="Email">[email protected]</FL>
<FL val="Title">Manager</FL>
<FL val="Phone">1234567890</FL>
<FL val="Home Phone">0987654321</FL>
<FL val="Other Phone">1212211212</FL>
<FL val="Fax">02927272626</FL>
<FL val="Mobile">292827622</FL>
</row>
</Leads>

I am trying to replicate it using a unit test in C#, but it gives me errors of all kinds.. i've lost all the different ways that i've tried, but i'll post what i currently have and the error it is throwing.

    [Test]
    public void TestMethod()
    {
        XmlDocument doc = new XmlDocument();
        XmlDeclaration declare = doc.CreateXmlDeclaration("1.0", null, null);
        doc.AppendChild(declare);
        doc.AppendChild(doc.CreateElement("Potentials"));
        doc.AppendChild(doc.CreateAttribute("FL", "AccountName", "Robert Snyder"));

        doc.Save("C:\\test.xml");
        //doc.WriteContentTo(XmlWriter.Create("C:\\test.xml"));
    }

Exception

System.InvalidOperationException : The specified node cannot be inserted as the valid child of this node, because the specified node is the wrong type. - c:\Subversion\ZohoApi\ZohoApi\Tests\XmlDataTests.cs:22

Please help me understand this a little better. I have been trying for a long time, and just can't figure it out.

2

There are 2 answers

0
Robert Snyder On BEST ANSWER

In order to make the following output

<Leads>
  <row no="1">
    <FL val="Lead Source">Web Download</FL>
    <FL val="First Name">Robert</FL>
    <FL val="Last Name">Snyder</FL>
    <FL val="Email">[email protected]</FL>
    <FL val="Title">Programmer</FL>
    <FL val="Phone">1029384756</FL>
    <FL val="Home Phone">6574839201</FL>
    <FL val="Other Phone">1243567890</FL>
    <FL val="Fax">098776545432</FL>
    <FL val="Mobile">1243098566</FL>
  </row>
</Leads>

copy use this code and run it in a unit test.

[TestFixture]
public class XmlDataTests
{
    XmlDocument doc = new XmlDocument();
    [Test]
    public void TestMethod()
    {
        var rootNode = doc.CreateElement("Leads");
        doc.AppendChild(rootNode);

        var rowNode = doc.CreateElement("row");
        var attribute = doc.CreateAttribute("no");
        attribute.Value = "1";
        rowNode.Attributes.Append(attribute);

        rowNode.AppendChild(GenerateNode("Lead Source","Web Download"));
        rowNode.AppendChild(GenerateNode("First Name","Robert"));
        rowNode.AppendChild(GenerateNode("Last Name","Snyder"));
        rowNode.AppendChild(GenerateNode("Email","[email protected]"));
        rowNode.AppendChild(GenerateNode("Title","Programmer"));
        rowNode.AppendChild(GenerateNode("Phone","1029384756"));
        rowNode.AppendChild(GenerateNode("Home Phone","6574839201"));
        rowNode.AppendChild(GenerateNode("Other Phone","1243567890"));
        rowNode.AppendChild(GenerateNode("Fax","098776545432"));
        rowNode.AppendChild(GenerateNode("Mobile","1243098566"));

        rootNode.AppendChild(rowNode);
        doc.Save("C:\\test.xml");
    }

    private XmlNode GenerateNode(string field, string innerValue)
    {
        var xmlNode = doc.CreateElement("FL");
        var xmlAttribute = doc.CreateAttribute("val");
        xmlAttribute.Value = field;
        xmlNode.Attributes.Append(xmlAttribute);
        xmlNode.InnerText = innerValue;

        return xmlNode;
    }
}
0
Jeff Watkins On

You're trying to add the attribute to the Document itself, not the root node. Cache your root element created by CreateElement and call AppendChild on that.