惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

AI
AI
TaoSecurity Blog
TaoSecurity Blog
H
Heimdal Security Blog
Help Net Security
Help Net Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Microsoft Azure Blog
Microsoft Azure Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Google DeepMind News
Google DeepMind News
爱范儿
爱范儿
The Cloudflare Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
人人都是产品经理
人人都是产品经理
大猫的无限游戏
大猫的无限游戏
N
News | PayPal Newsroom
V2EX - 技术
V2EX - 技术
博客园 - 【当耐特】
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
Secure Thoughts
C
CERT Recently Published Vulnerability Notes
罗磊的独立博客
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Privacy & Cybersecurity Law Blog
有赞技术团队
有赞技术团队
S
Schneier on Security
S
SegmentFault 最新的问题
Google Online Security Blog
Google Online Security Blog
H
Hacker News: Front Page
The Last Watchdog
The Last Watchdog
Schneier on Security
Schneier on Security
PCI Perspectives
PCI Perspectives
IT之家
IT之家
Project Zero
Project Zero
博客园 - 司徒正美
P
Privacy International News Feed
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Jina AI
Jina AI
Security Latest
Security Latest
Hacker News - Newest:
Hacker News - Newest: "LLM"
腾讯CDC
C
CXSECURITY Database RSS Feed - CXSecurity.com
阮一峰的网络日志
阮一峰的网络日志
C
Check Point Blog
aimingoo的专栏
aimingoo的专栏
V
Vulnerabilities – Threatpost
W
WeLiveSecurity
NISL@THU
NISL@THU
Webroot Blog
Webroot Blog
N
Netflix TechBlog - Medium
L
Lohrmann on Cybersecurity

博客园 - KeithDan

Windows 8开发 WinRT 对ZIP文件解压缩及文件夹的ZIP压缩 wp应用发布,金宝贝点读 WP7游戏分裂细胞 WP7游戏方块夺宝 xeno tactic 2 完整版 Lua扫雷 惊人!天下奇闻“鹏” 今天很郁闷 HighLightCode1.2——开源项目 代码着色--关于循环与分组 代码着色--开源系列 利用非托管dll实现多平台加密 非托管dll的需要注意的地方 page_load执行两次 transfer object (3) transfer object (2) transfer object (1) 中国象棋(网络版) 人生的阶梯
C#手工解析XML
KeithDan · 2012-04-26 · via 博客园 - KeithDan

唉,已经好久没逛园子了,今天没事来写一点

前段时间遇到个情况,由于引用的程序集(为XNA wp的)所依赖的System.XML的版本不一样,导致无法使用系统的XML解析。本来想从网上找个自己实现XML算法的程序,还真不好找,不过也是,谁没事去写这个东西。

没办法,就自己写了,当然,这个比较简单,因为我所使用的就这个简单功能,有需要的朋友可以看看,也就免得以后不好找了,根据需要可以自由扩充。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace GameEngine.XNA
{
    /// <summary>
    
/// XML解析器
    
/// </summary>
    public class XmlParser
    {
        /// <summary>
        
/// 加载XML
        
/// </summary>
        
/// <param name="xml"></param>
        public void Load(string xml)
        {
            InnerXML = xml;

            Root = new XmlNode();
            Root._internalXML = InnerXML;
            Root.Parser();
        }
        public string InnerXML { getset; }
        /// <summary>
        
/// 根节点
        
/// </summary>
        public XmlNode Root { getset; }
    }

    /// <summary>
    
/// XML属性
    
/// </summary>
    public class XmlAttribute
    {
        public string Name { getset; }
        public string Value { getset; }
        internal string _internalXML;
        internal void Parser()
        {
            string[] exp = _internalXML.Split('=');
            Name = exp[0];
            Value = exp[1].Substring(1, exp[1].Length - 2);
        }
    }

    /// <summary>
    
/// XML节点
    
/// </summary>
    public class XmlNode
    {

        public string Name { getset; }
        public string Value { getset; }
        /// <summary>
        
/// 子节点
        
/// </summary>
        public List<XmlNode> Nodes { getset; }
        public List<XmlAttribute> Attributes { getset; }
        public string InnerXML { getset; }
        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;
        }

    }
}