使用XmlTextWriter和XmlTextReader操作Xml文件

具体用法参考如下代码

XmlTextWriter:

XmlTextWriter writer = new XmlTextWriter(strmMetadata, Encoding.UTF8) {
Formatting = Formatting.Indented
};
writer.WriteStartDocument();
writer.WriteStartElement("Session");
writer.WriteAttributeString("SID", this.id.ToString());
writer.WriteAttributeString("BitFlags", ((uint) this.BitFlags).ToString("x"));

writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();



XmlTextReader:

代码分析:
XmlTextReader reader = new mlTextReader(strmMetadata);
while (reader.Read())
{
string str3;
if ((reader.NodeType == XmlNodeType.Element) && ((str3 = reader.Name) != null))
{
if (!(str3 == "Session"))
{
if (str3 == "SessionFlag")
{
goto Label_00BB;
}
if (str3 == "SessionTimers")
{
goto Label_00E1;
}
if (str3 == "PipeInfo")
{
goto Label_0292;
}
}
else
{
}
}
continue;
Label_00BB:
this.oFlags.Add(reader.GetAttribute("N"), reader.GetAttribute("V"));
continue;
Label_00E1:
this.Timers.ClientConnected = XmlConvert.ToDateTime(reader.GetAttribute("ClientConnected"), XmlDateTimeSerializationMode.RoundtripKind);
string attribute = reader.GetAttribute("ClientBeginRequest");
if (attribute != null)
{
this.Timers.ClientBeginRequest = XmlConvert.ToDateTime(attribute, XmlDateTimeSerializationMode.RoundtripKind);
}
reader.Close();



1,使用while循环配合continue,goto语句来读取节点信息。

2,使用GetAttribute函数时,结果!=null的判断。

你可能感兴趣的:(Writer)