So I'm reading a file from XML and this works pretty well using the Debug-Output.
我正在从XML中读取一个文件,使用调试输出可以很好地工作。
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element && reader.Name == "Note")
{
Note n = new Note();
reader.ReadToFollowing("NoteTitle");
string s = reader.ReadElementContentAsString();
n.NoteTitle = s;
Debug.WriteLine("s " + s);
Debug.WriteLine("n " + n.NoteTitle);
}
}
My problem is that
我的问题是,
n.NoteTitle = s;
doesn't do anything so the second Debug prints only "n " and nothing else, while the first debug prints "s Notetitle1"
correctly.
不做任何操作,因此第二个调试只打印“n”,而第一个调试只打印“Notetitle1”。
What is my problem?
我的问题是什么?
Edit: Sorry, implementation of NoteTitle:
编辑:对不起,实现NoteTitle:
private string _noteTitle = string.Empty;
public string NoteTitle
{
get { return this._noteTitle; }
set { RaisePropertyChanged("NoteTitle"); }
}
2 个解决方案
#1
3
Looking at your implementation of the NoteTitle
setter, you are forgetting to set the value of the private field i.e.
查看NoteTitle setter的实现,您忘记设置private字段的值。
private string _noteTitle = string.Empty;
public string NoteTitle
{
get { return this._noteTitle; }
set
{
this._noteTitle = value; // set the field value
RaisePropertyChanged("NoteTitle");
}
}
This would explain why no value is being retained when you set the NoteTitle
property.
这将解释为什么在设置NoteTitle属性时没有保留值。
#2
2
The serialization in the OP seems to be throwing people off. I think the problem would be better expressed as:
OP中的序列化似乎让人很反感,我认为问题最好表述为:
Note n = new Note();
string s = "TEST";
n.NoteTitle = s;
Debug.WriteLine("s " + s);
Debug.WriteLine("n " + n.NoteTitle);
The question becomes: Why does the second WriteLine() show "n " and not "n TEST"?
问题变成:为什么第二个WriteLine()显示“n”而不是“n TEST”?
The answer is that there is something peculiar about the implementation of either the setter or the getter for Note.NoteTitle. For example, this implementation would have the effect:
答案是,对于Note.NoteTitle的setter或getter的实现有一些特殊的地方。例如,这项执行将产生以下效果:
public string NoteTitle
{
get
{
return "";
}
set
{
// Do nothing.
}
}
#1
3
Looking at your implementation of the NoteTitle
setter, you are forgetting to set the value of the private field i.e.
查看NoteTitle setter的实现,您忘记设置private字段的值。
private string _noteTitle = string.Empty;
public string NoteTitle
{
get { return this._noteTitle; }
set
{
this._noteTitle = value; // set the field value
RaisePropertyChanged("NoteTitle");
}
}
This would explain why no value is being retained when you set the NoteTitle
property.
这将解释为什么在设置NoteTitle属性时没有保留值。
#2
2
The serialization in the OP seems to be throwing people off. I think the problem would be better expressed as:
OP中的序列化似乎让人很反感,我认为问题最好表述为:
Note n = new Note();
string s = "TEST";
n.NoteTitle = s;
Debug.WriteLine("s " + s);
Debug.WriteLine("n " + n.NoteTitle);
The question becomes: Why does the second WriteLine() show "n " and not "n TEST"?
问题变成:为什么第二个WriteLine()显示“n”而不是“n TEST”?
The answer is that there is something peculiar about the implementation of either the setter or the getter for Note.NoteTitle. For example, this implementation would have the effect:
答案是,对于Note.NoteTitle的setter或getter的实现有一些特殊的地方。例如,这项执行将产生以下效果:
public string NoteTitle
{
get
{
return "";
}
set
{
// Do nothing.
}
}