【发布时间】:2015-12-10 16:47:58
【问题描述】:
我有一个使用 Web 服务的控制台应用程序。我正在努力实现它以提取有关工作正常的项目的一些信息。
网络服务GetProjectData() 输出几个参数,其中一个是状态。
我创建了一个具有我需要的状态的 xml 文件,因为 GetProjectData() 不接受任何参数。
当我运行以下代码时出现错误There is an error in XML document (0, 0).
你能指点我如何做到这一点吗?
谢谢
这是创建的xml
<root>
<status>
<List>Published</List>
<List>expired</List>
</status>
</root>
这是我的代码
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());
}
}
这里有我预期的输出。
【问题讨论】:
-
您期望的输入/输出是什么?
-
您正在尝试从输出文件中反序列化。
ser.Deserialize(fs)其中fs是new FileStream(outCsvFile, FileMode.Append, FileAccess.Write)。
标签: c# xml console-application streamwriter xmlserializer