Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C#

Options
  • 11-02-2010 8:21pm
    #1
    Registered Users Posts: 7,681 ✭✭✭


    I am looking to create the following xml file
    <?xml version="1.0" encoding="utf-8"?>
    <document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <emailAddress>xxx@yy.zz</emailAddress>
      <docRequest>
        <documentID>12345</documentID>
        <documentID>12346</documentID>
      </docRequest>
      <requestID>123</requestID>
      <returnLocation>c:\abc\</returnLocation>
      <status>1</status>
    </document>
    

    I have created the following code to do it.
    using System;
    using System.Xml;
    using System.Xml.Serialization;
    using System.IO;
    using System.Collections;
    using System.Collections.Generic;
    using System.Text;
    using System.Reflection;
    
    namespace ConsoleApplication2
    {
        [XmlRootAttribute("document", IsNullable = false)]
        public class document
        {
            public string emailAddress;
            public docRequest[] docid;
    
            public int requestID;
            public string returnLocation;
            public int status;
        }
    
        public class docRequest
        {
                public int[] docid;
        }
    
        class Program
        {
            static void Main(string[] args)
            {
    
                ArrayList _ar = new ArrayList();
                _ar.Add(12345);
                _ar.Add(12346);
    
                XmlSerializer serializer =
                new XmlSerializer(typeof(document));
                TextWriter writer = new StreamWriter(@"D:\Test.xml");
                document doc = new document();
    
                docRequest[] myArr = (docRequest[])_ar.ToArray(typeof(docRequest));
    
                doc.docid = myArr;
                doc.requestID = 123;
                doc.status = 1;
                doc.returnLocation = @"c:\abc\";
                doc.emailAddress = "xxx@yyy.zz";
    
                serializer.Serialize(writer, doc);
                writer.Close();
    
            }
        }
    }
    

    I am getting the following error on the line docRequest[] myArr = (docRequest[])_ar.ToArray(typeof(docRequest));

    At least one element in the source array could not be cast down to the destination array type.

    Can anyone help me please


Comments

  • Registered Users Posts: 515 ✭✭✭NeverSayDie


    Trampas wrote: »
    ...
        public class docRequest
        {
                public int[] docid;
        }
    
    ...
    
                ArrayList _ar = new ArrayList();
                _ar.Add(12345);
                _ar.Add(12346);
    ...
    
                docRequest[] myArr = (docRequest[])_ar.ToArray(typeof(docRequest));
    
    ...
    

    I am getting the following error on the line docRequest[] myArr = (docRequest[])_ar.ToArray(typeof(docRequest));

    At least one element in the source array could not be cast down to the destination array type.

    Can anyone help me please

    Well, I'd guess the problem there is that you have an ArrayList with some integers in it, and you're trying to convert that to an array of docRequest objects, but the Framework doesn't know how to cast integers into docRequest objects. You'll need to find a better way of populating myArr. Loop over a jagged array (or generic of some sort) of ints and instantiate docRequests into a collection I guess, then turn that into your array.

    I've no idea if your code as a whole will produce that XML btw, I haven't used the XmlSerializer classes before. You're probably not going to get "documentID" nodes from anywhere though, there doesn't seem to be any property with that name. I'm not sure if you need an array of docRequests at all, if there's only supposed to be one collection of "documentID"s for each "document". A single docRequest member with an array of ints in it would presumably cover that.


Advertisement