Alas, I haven't visited the garden for a long time. I have nothing to do today.
Some time ago, due to the different versions of System. XML that the referenced assembly (XNA wp) depends on, the XML parsing of the System cannot be used. I wanted to find a program to implement the XML algorithm on the Internet, but it's really hard to find it, but no one has to write it.
No way, I wrote it myself. Of course, this is relatively simple, because I use this simple function. If you need it, you can check it out, so that you will not be able to find it in the future, you can expand it as needed.
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Collections;
Namespace GameEngine. XNA
{
/// <Summary>
/// XML Parser
/// </Summary>
Public class XmlParser
{
/// <Summary>
/// Load XML
/// </Summary>
/// <Param name = "xml"> </param>
Public void Load (string xml)
{
InnerXML = xml;
Root = new XmlNode ();
Root. _ internalXML = InnerXML;
Root. Parser ();
}
Public string InnerXML {get; set ;}
/// <Summary>
/// Root Node
/// </Summary>
Public XmlNode Root {get; set ;}
}
/// <Summary>
/// XML attributes
/// </Summary>
Public class XmlAttribute
{
Public string Name {get; set ;}
Public string Value {get; set ;}
Internal string _ internalXML;
Internal void Parser ()
{
String [] exp = _ internalXML. Split ('= ');
Name = exp [0];
Value = exp [1]. Substring (1, exp [1]. Length-2 );
}
}
/// <Summary>
/// XML Node
/// </Summary>
Public class XmlNode
{
Public string Name {get; set ;}
Public string Value {get; set ;}
/// <Summary>
/// Subnode
/// </Summary>
Public List <XmlNode> Nodes {get; set ;}
Public List <XmlAttribute> Attributes {get; set ;}
Public string InnerXML {get; set ;}
Internal string _ internalXML;
Private int start;
Private int end;
Internal int current;
Public XmlNode ()
{
Nodes = new List <XmlNode> ();
Attributes = new List <XmlAttribute> ();
}
Private void ParserXML ()
{
InnerXML = InternalParserXML ();
}
Private string InternalParserXML ()
{
StringBuilder builder = new StringBuilder ();
Builder. Append ("<");
Builder. Append (Name );
For (int I = 0; I <Attributes. Count; I ++)
{
Builder. Append ("");
Builder. Append (Attributes [I]. Name );
Builder. Append ("= ");
Builder. Append ("\"");
Builder. Append (Attributes [I]. Value );
Builder. Append ("\"");
}
Builder. Append ("> ");
For (int I = 0; I <Nodes. Count; I ++)
{
Builder. Append (Nodes [I]. InternalParserXML ());
}
Builder. Append ("</");
Builder. Append (Name );
Builder. Append ("> ");
Return builder. ToString ();
}
Internal void Parser ()
{
NodeType type = GetCurrentNodeType ();
ReadBeginBlock ();
While (true)
{
Type = GetCurrentNodeType ();
If (type = NodeType. BeginNode)
{
XmlNode node = new XmlNode ();
Node. _ internalXML = _ internalXML; //. Substring (current, _ internalXML. Length-current );
Node. start = current;
Node. current = current;
Node. Parser ();
Nodes. Add (node );
Current = node. end;
}
Else if (type = NodeType. Text)
{
ReadValue ();
}
Else if (type = NodeType. EndNode)
{
ReadEndBlock ();
Break;
}
}
}
Public enum NodeType
{
BeginNode,
EndNode,
Text
}
Private NodeType GetCurrentNodeType ()
{
If (_ internalXML [current] = '<' & _ internalXML [current + 1]! = '/')
{
Return NodeType. BeginNode;
}
Else if (_ internalXML [current] = '<' & _ internalXML [current + 1] = '/')
{
Return NodeType. EndNode;
}
Else if (_ internalXML [current]! = '<')
{
Return NodeType. Text;
}
Throw new Exception ();
}
Private void ReadBeginBlock ()
{
Current ++;
Int posend = _ internalXML. IndexOf ('>', start );
String nodeinner = _ internalXML. Substring (current, posend-current );
String [] atts = nodeinner. Split ('');
Name = atts [0];
If (atts. Length> 1)
{
For (int I = 1; I <atts. Length; I ++)
{
XmlAttribute att = new XmlAttribute ();
Att. _ internalXML = atts [I];
Att. Parser ();
Attributes. Add (att );
}
}
Posend ++;
Current = posend;
}
Private void ReadValue ()
{
Int posend = _ internalXML. IndexOf ('<', current );
Value = _ internalXML. Substring (current, posend-current );
Current = posend;
}
Private void ReadEndBlock ()
{
Int posend = _ internalXML. IndexOf ('>', current );
Posend ++;
Current = posend;
InnerXML = _ internalXML. Substring (start, current-start );
End = current;
}
}
}