C # parsing XML manually

Source: Internet
Author: User

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;
}

}
}

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.