• QQ
  • nahooten@sina.com
  • 常州市九洲新世界花苑15-2

游戏开发

U3D非xml的字符串变规范的xml

原创内容,转载请注明原文网址:http://homeqin.cn/a/wenzhangboke/jishutiandi/youxikaifa/2018/1117/191.html

Unity3d-XML将一个”xml的字符串变成规范的xml格式

 

有时候xml文件出错,主要原因是没有把xmlString串做成规范化的处理,为此,我们常州网站开发-幻天网络本篇文章就给大家介绍下XML将一个”xml的字符串变成规范的xml格式的实现方法。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Text.RegularExpressions;

using System.Threading.Tasks;

using System.Xml;

using System.Xml.Linq;

namespace 批量替换字符串

    public static class Extensions

   

        /// <summary>

        /// 获取指定名称的xml属性值

        /// </summary>

        /// <param name="e"></param>

        /// <param name="name"></param>

        /// <returns>失败返回null</returns>

        public static string AttributeValue(this XElement e, XName name)

       

            if (e != null)

           

                var a = e.Attribute(name);

                if (a != null)

                    return a.Value;

           

            return null;

       

   

    class Program

   

        /// <summary>

        /// xmlattribute加上引号

        /// </summary>

        /// <param name="poorXml"></param>

        /// <returns></returns>

        private static string RichXmlFix(string poorXml)

       

            var xml = Regex.Replace(poorXml, @"\<[^<>]*=[^<>]*\>", mb =>

           

                return Regex.Replace(mb.Value, @"\w+=(?<attr>\w+)", ma =>

               

                    var a = ma.Groups["attr"];

                    var idx = a.Index - ma.Index;

                    var len = a.Length;

                    return string.Format("0\"1\"2", ma.Value.Substring(0, idx), a.Value, ma.Value.Substring(idx + len));

                );

            );

            return xml;

       

        static void Main(string[] args)

       

            string xml = "<p><n color=GXColorYellow font=\"3\"> 2级)王者之剑</n></p><p><image pack=\"INTERFACE_PACK\" infoname=\"INTERFACE_40_4_P\"></image></p><p><n></n><n color=\"GXColorYellow\" font=\"3\">任务进程:</n><n></n></p><p><n>   去拔出</n><goto id=\"60141\" mapid=\"1\"/><n>后,回复</n><goto id=\"60046\" mapid=\"1\"/><n></n></p><p><newline/></p><p><n></n><n color=\"GXColorYellow\" font=\"3\">任务描述:</n><n></n></p><p><n>   星之城的</n><n color=\"GXColorGreen\">王者之剑</n><n>是力量与权力的象征。只有真正的王者才能把它拔出。你去试试吧。</n></p><p><newline></newline></p><p><n color=\"GXColorYellow\" font=\"3\">任务奖励</n></p><p><button packid=\"0\" group=\"5\" frame=\"40\" id=\"-1\">400 经验</button></p><p><button packid=\"0\" group=\"5\" frame=\"53\" id=\"-1\">5 成长勋章</button></p>";

            Console.WriteLine(Convert(xml));

            Console.Read();

       

        static string Convert(string xml)

       

            var str = new XElement("root", Convert(XDocument.Parse("<root>" + RichXmlFix(xml) + "</root>").Root.Elements())).ToString();

            str = str.Substring(6, str.Length - 13);

            return str;

       

        static IEnumerable<XElement> Convert(IEnumerable<XElement> xml)

       

            foreach (var e in xml)

           

                switch (e.Name.ToString())

               

                    case "p":

                       

                            var firstChildNode = e.FirstNode;

                            if (firstChildNode != null)

                                yield return new XElement("p", Convert(e.Elements()));

                            else

                                yield return e;

                       

                        break;

                    case "n":

                       

                            if (e.Attribute("color") != null)

                           

                                yield return new XElement("n",

                                    new XElement("color", e.Value, new XAttribute("value", ConverColor(e.AttributeValue("color")))));

                           

                            else

                                yield return e;

                       

                        break;

                    case "button":

                       

                            yield return new XElement("img", new XAttribute("atlas", "Atlases/SkillIcon"), new XAttribute("sprite", "1002"));

                            yield return new XElement("n", e.Value);

                       

                        break;

                    default:

                        yield return e;

                        break;

               

           

       

        private static string ConverColor(string color)

       

            color = color.Substring(color.IndexOf("GXColor") + 7).ToLower();

            return color;

       

   



上篇:上一篇:UGUI Text组件实际文本宽高的获取
下篇:下一篇:U3d WWW实现图片资源保存和本地加载