本文整理汇总了C#中System.Xml.XmlNodeReader.ReadStartElement方法的典型用法代码示例。如果您正苦于以下问题:C# XmlNodeReader.ReadStartElement方法的具体用法?C# XmlNodeReader.ReadStartElement怎么用?C# XmlNodeReader.ReadStartElement使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Xml.XmlNodeReader
的用法示例。
在下文中一共展示了XmlNodeReader.ReadStartElement方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: convertFromXml
/// <summary>
/// Removing this from the IXmlSerializable Implementation because engineers keep exposing the SerialzableDictionary object
/// and that cannot be allowed because nobody else can deserialize. It is for persistance only so the logic was moved here.
/// </summary>
/// <param name="doc">The doc.</param>
/// <returns></returns>
private IList<string> convertFromXml(XmlDocument doc)
{
XmlReader reader = new XmlNodeReader(doc);
//First check empty element and return if found
bool wasEmpty = reader.IsEmptyElement;
reader.Read();
if (wasEmpty)
return null;
IList<string> result = new List<string>();
reader.ReadStartElement(DocumentElement);
if (reader.NodeType != XmlNodeType.Element)
return result;
//Loop through the nodes representing this object in the reader
while (reader.NodeType != XmlNodeType.EndElement)
{
//First try the optimized format
//string key = reader.GetAttribute(KEY_ATTRIBUTE);
string typeName = reader.GetAttribute(TYPE_ATTRIBUTE);
//Read the Xml element representing a new key & value pair
reader.ReadStartElement(ITEM_ELEMENT);
//if (String.IsNullOrEmpty(key))
//{
// //Key is not being stoed as an attribute - this must be xml in the old format
// //KEEP THIS CODE FOR BACKWARDS COMPATIBILITY
// //First we need to get the type of the object written to the Xml during serialization so that
// //we can create a new serializer that understands how to deserialize this type.
// typeName = reader.GetAttribute(TYPE_ATTRIBUTE);
// //Read the Xml element representing the key in this key & value pair
// reader.ReadStartElement(KEY_ELEMENT);
// //Allright now create the serializer and deserialize the defined object type
// XmlSerializer keySerializer = new XmlSerializer(typeof(string));
// //key = (string)keySerializer.Deserialize(reader);
// if (key == null)
// throw new ApplicationException(String.Format("Null key encountered on line {0}",
// reader.Depth));
// //Read the end of the key element
// reader.ReadEndElement();
//}
Type valuetype = (typeName != null ? Type.GetType(typeName) : typeof(object)) ?? typeof(object);
//Read the Xml element representing the value in this key & value pair
reader.ReadStartElement(VALUE_ELEMENT);
//Now create the serialize and deserialize the object type defined from the type attribute
XmlSerializer valueSerializer = new XmlSerializer(valuetype);
//HACK!!!
//Make sure you catch any errors caused by invalid types cannot be deserialized. For example this whole process
//kept blowing up because the type ould not be serialized.
string value;// = (TValue) valueSerializer.Deserialize(reader);
try { value = (string)valueSerializer.Deserialize(reader); }
catch (Exception)
{
value = default(string);
//skip top the end of the current element
reader.Skip();
}
//Read the end of the value element
reader.ReadEndElement();
//Now add the deserialized objects to the hashtable.
result.Add(value);
//Read the end of the element holding the key and value elements
if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName == ITEM_ELEMENT)
reader.ReadEndElement();
reader.MoveToContent();
}
//All done - read the ending element
reader.ReadEndElement();
return result;
}