There is an error in XML document (0, 0). console application C#

时间:2015-09-14 15:53:52

标签: c# xml console-application streamwriter xmlserializer

I have a console application using web services. What I am trying to achieve its to extract several information about projects which works fine.

The web service GetProjectData() outputs several parameters and one of those its the status.

I have created a xml file with the status that I need because the GetProjectData() does not take any arguments.

When I run the following code I have an error There is an error in XML document (0, 0).

Can you please point me how to achieve this?

Thanks

This is the xml created

<root>
    <status>
        <List>Published</List>
        <List>expired</List>
    </status>
</root>

Here is my code

class ProjectEqualityComparer : IEqualityComparer<WS.ProjectMetaData>
    {
        #region IEqualityComparer<Project> Members

        public bool Equals(WS.ProjectMetaData x, WS.ProjectMetaData y)
        {
            return x.ProjectID.Equals(y.ProjectID);
        }

        public int GetHashCode(WS.ProjectMetaData obj)
        {
            return obj.ProjectID.GetHashCode();
        }

        #endregion
    }

[XmlRoot("root")]
public class ListOFStatus
{
    public ListOFStatus() { Items = new List<Status>(); }
    [XmlElement("status")]
    public List<Status> Items { get; set; }
}

public class Status
{
    [XmlElement("List")]
    public String Name { get; set; }
}


string customerList = "customerList";
string outCsvFile = string.Format(@"C:\\customerList\\{0}.csv", customerList + DateTime.Now.ToString("_yyyyMMdd HHmms"));

XmlSerializer ser = new XmlSerializer(typeof(ListOFStatus));
List<ListOFStatus> list = new List<ListOFStatus>();
object obj = null;

WS.ProjectData[] pr = db.GetProjectData();

using (FileStream fs = new FileStream(outCsvFile, FileMode.Append, FileAccess.Write))                    
using (StreamWriter file = new StreamWriter(fs))
{
    file.WriteLine("ProjectID, ProjectTitle,PublishStatus,NumberOfCustomers,ProjectStartDate,ProjectEndDate, URL");
    foreach(WS.ProjectData proj in pr.Distinct(new ProjectEqualityComparer()))
    {
        var userIDs = db.GetList(proj.ProjectID);                
        file.WriteLine("{0},\"{1}\",{2},{3},{4},{5},{6}",
        proj.ProjectID,
        proj.ProjectTitle,
        obj = ser.Deserialize(fs), //where I need to display the status from the projects either Published or expired
        userIDs.Length.ToString(NumberFormatInfo.InvariantInfo),
        proj.ProjectStartDate.ToString(DateTimeFormatInfo.InvariantInfo),
        proj.ProjectEndDate.ToString(DateTimeFormatInfo.InvariantInfo),
        url[i].ToString());
     }
}

Here you have my expected output.

Expected Output

1 个答案:

答案 0 :(得分:0)

You are trying to deserialize from your output file. You have

using (FileStream fs = new FileStream(outCsvFile, FileMode.Append, FileAccess.Write))                    
// ...
{
    // ...
    obj = ser.Deserialize(fs);
    // ...
}

Since the FileStream is opened in Append Mode, there is nothing to read.

Furthermore, you are writing to that same file, which seems very wrong.

UPDATE:

You would need to do something along the lines of:

using (FileStream xml = File.OpenRead("xmlfilename"))
using (FileStream fs = new FileStream(outCsvFile, FileMode.Append, FileAccess.Write))
// ...
{
    // ...
    obj = ser.Deserialize(xml);
    // ...
}