System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
Byte[] pBytes = encoding.GetBytes(strXML);
MemoryStream pMem = new MemoryStream(pBytes, 0, pBytes.Length, true, true);
XmlReader reader = XmlReader.Create(pMem);
string s = Encoding.UTF8.GetString(pMem.GetBuffer(), 0, (int)pMem.Position);
XmlElement element;
XmlDocument xmlDoc = new XmlDocument();
pMem.Seek(0, SeekOrigin.Begin);
while (reader.Read())
{
XmlNode node = null;
switch (reader.NodeType)
{
case XmlNodeType.Element:
bool fEmptyElement = reader.IsEmptyElement;
element = xmlDoc.CreateElement(reader.Prefix, reader.LocalName, reader.NamespaceURI);
//element.IsEmpty = fEmptyElement;
//ReadAttributes(reader, element);
//if (!fEmptyElement) {
//parent.AppendChild(element);
//parent = element;
//continue;
//}
//node = element;
break;
case XmlNodeType.EndElement:
//if (parent.ParentNode == null)
//{
// syntax error in document.
// IXmlLineInfo li = (IXmlLineInfo)reader;
// throw new XmlException(string.Format(SR.UnexpectedToken,""+reader.LocalName+">", li.LineNumber, li.LinePosition), null, li.LineNumber, li.LinePosition);
// }
//parent = parent.ParentNode;
continue;
case XmlNodeType.EntityReference:
if (reader.CanResolveEntity)
{
reader.ResolveEntity();
}
continue;
case XmlNodeType.EndEntity:
continue;
case XmlNodeType.Attribute:
//node = LoadAttributeNode(parent);
break;
case XmlNodeType.Text:
node = xmlDoc.CreateTextNode(reader.Value);
break;
case XmlNodeType.SignificantWhitespace:
node = xmlDoc.CreateSignificantWhitespace(reader.Value);
break;
case XmlNodeType.Whitespace:
//if (preserveWhitespace) {
// node = xmlDoc.CreateWhitespace(reader.Value);
// break;
//}
//else {
//continue;
//}
continue;
case XmlNodeType.CDATA:
node = xmlDoc.CreateCDataSection(reader.Value);
break;
case XmlNodeType.XmlDeclaration:
break;
case XmlNodeType.ProcessingInstruction:
node = xmlDoc.CreateProcessingInstruction(reader.Name, reader.Value);
break;
case XmlNodeType.Comment:
node = xmlDoc.CreateComment(reader.Value);
break;
case XmlNodeType.DocumentType:
string pubid = r.GetAttribute("PUBLIC");
string sysid = r.GetAttribute("SYSTEM");
node = xmlDoc.CreateDocumentType(reader.Name, pubid, sysid, reader.Value);
break;
default:
//UnexpectedNodeType(reader.NodeType);
break;
}
}
Wednesday, October 31, 2012
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment