我正在尝试使用 StreamWriter 通过方法序列化对象,但不知何故我无法动态定义文件的路径。
例子:
public void SerializeToXML(Record aRecord)
{
XmlSerializer serializer = new XmlSerializer(typeof(Movie));
var path = string.Format("@\"{0}\\{1}.xml\"", "C:\\Objects", aRecord.GetHashCode());
TextWriter textWriter = new StreamWriter(path);
serializer.Serialize(textWriter, movie);
textWriter.Close();
}
然后它说:"Illegal characters in path" on the line :TextWriter textWriter = new StreamWriter(path);
当我静态传递它时,它会使用这种格式,但是当我想动态传递它时,它不接受它。