سؤال

i want to deserialize an xml file and copy those values into csv file.i tried like this.

 [Serializable, XmlRoot("Configuration"), XmlType("Configuration")]
        public class LabelRequest
        {
           public string weightoz { get; set; }
           public string MailClass { get; set; }

            public static void DeSerialization()
            {
               LabelRequest label = new LabelRequest();
               TextReader txtReader = new StreamReader(@"C:\xmlfile.xml");
               XmlSerializer xmlSerializer = new XmlSerializer(typeof(LabelRequest));
               label = (LabelRequest) xmlSerializer.Deserialize(txtReader);
               txtReader.Close();
            }

        }

and the xml file is as follows

<Labelrequest>
     <weightoz>2</weightoz>
     <mailclass>abc</mailclass>
</labelrequest>
هل كانت مفيدة؟

المحلول

To write the values to a CSV file shouldn't be too difficult. Your example does not contain any code which writes to a file though. It only deserialises an XML file. Might I suggest something like this.

    public static class LabelRequestSerializer
    {
        public static Label DeserializeXmlFile(string fileName)
        {
           using (TextReader txtReader = new StreamReader(fileName))
           {
               XmlSerializer xmlSerializer = new XmlSerializer(typeof(LabelRequest));
               LabelRequest label = (LabelRequest) xmlSerializer.Deserialize(txtReader);
           }
        }

        public static void SerializeToCsv(LabelRequest labelRequest, string fileName)
        {
             if (labelRequest == null)
                throw new ArgumentNullException("labelRequest");

             StringBuilder sb = new StringBuilder();
             sb.Append(labelRequest.weightoz);
             sb.Append(",");
             sb.Append(labelRequest.mailclass);
             sb.AppendLine();

             using (StreamWriter stream = new StreamWriter(fileName))
             {
                 stream.Write(sb.ToString());
             }
        }
    }

Then you can pass the instance of LabelRequest you want to serialize to these static methods. That way LabelRequest does not know about how to serialize itself from files, which is a nice seperation of concerns. Like this

void SomeMethod()
{
    LabelRequest labelRequest = new LabelRequest();
    LabelRequestSerializer.SerializeToCsv(labelRequest, @"C:\Path\Goes\Here\label.csv");
}

Edit...

If you really don't want to write out every property manually you can use reflection. However there will be a performance hit by using this. Shouldn't be a problem compared to the file IO though.

public static void SerializeToCsv(LabelRequest labelRequest, string fileName)
        {
             if (labelRequest == null)
                throw new ArgumentNullException("labelRequest");

             StringBuilder sb = new StringBuilder();

            foreach (PropertyInfo info in labelRequest.GetType() .GetProperties()) 
            {
                   object value =   info.GetValue(labelRequest, null);
                    sb.Append(value);
                    sb.Append(", ");
             } 

             sb.AppendLine();
             using (StreamWriter stream = new StreamWriter(fileName))
             {
                 stream.Write(sb.ToString());
             }
        }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top