Coder Social home page Coder Social logo

haoljp / sixants.xmlfile Goto Github PK

View Code? Open in Web Editor NEW

This project forked from lin2code/xmlmodel

0.0 1.0 0.0 18 KB

一个快速创建、读取、编辑Xml文件的类XmlFile,非常简单易用。A quickly Create/Read/Edit Xml file Class XmlFile, very simple to use..

C# 100.00%

sixants.xmlfile's Introduction

SixAnts.XmlFile

一个快速创建、读取、编辑Xml文件的类XmlFile。使用非常简单。
再也不用去查XmlDocument、XmlNode、XmlElement类的用法了。
用法如下:以下代码可以到测试项目中查看

A quickly Create/Read/Edit Xml file Class XmlFile.Very simple to use.
No more XmlDocument/XmlNode/XmlElement, never search thoese class usage again.
how to use:code below can find in the test project

    class Program
    {
        static void Main(string[] args)
        {
            //两个标签类型:XmlChildTag和XmlBaseTag
            //XmlChildTag是子标签内部必须有子标签或基础标签,即BaseTagList或ChildTagList不能同时为空
            //XmlBaseTag是基础标签,内部不能有子标签

            //Two type tag:XmlChildTag and XmlBaseTag
            //XmlChildTag is childtag type must have child tag, means BaseTagList and ChildTagList can't both empty
            //XmlBaseTag is basetag type, can't have child tag insie
            
            Console.Read();
        }

        //创建XML文件
        public static void Create()
        {
            //必须创建一个根节点,可以是XmlChildTag或XmlBaseTag
            //must create a roottag, could be XmlChildTag or XmlBaseTag
            XmlChildTag rootTag = new XmlChildTag("Root");
            rootTag.BaseTagList.Add(new XmlBaseTag("BaseTagOne"));
            //XmlBaseTag rootTag = new XmlBaseTag("BaseRoot");
            XmlFile myXmlFile = new XmlFile("D:\\XML\\", "MyXmlFile.xml", "utf-8", rootTag);
        }

        //读取XML文件
        public static void Read()
        {
            XmlFile myXml = new XmlFile("D:\\XML\\", "MyXmlFile.xml");
            Console.WriteLine(myXml.Root.Name);
            Console.WriteLine((myXml.Root as XmlChildTag).BaseTagList.First().Name);
        }

        //修改XML文件
        public static void Edit()
        {
            XmlFile myXml = new XmlFile("D:\\XML\\", "MyXmlFile.xml");
            //修改root
            //Edit root
            XmlChildTag root = myXml.Root as XmlChildTag;
            root.Name = "NewRoot";
            root.Attrs.Add("attr1", "value1");
            //添加子节点
            //add child tag
            XmlChildTag newChild = new XmlChildTag("NewChild");
            newChild.BaseTagList.Add(new XmlBaseTag("BaseTagTwo"));
            root.ChildTagList.Add(newChild);
            myXml.Save();
        }

        #region 快速获取测试前QuicklyGet.xml文件内容/QuicklyGet.xml file content before QuicklyGet test
        /* 
        <?xml version="1.0" encoding="utf-8"?>
        <Root>
          <BaseTagOne>
          </BaseTagOne>
          <ChildA>
            <BaseTagTwo>
            </BaseTagTwo>
            <BaseTagTwo>
            </BaseTagTwo>
            <BaseTagTwo>
            </BaseTagTwo>
          </ChildA>
          <ChildB>
            <ChildC name="cone">
              <BaseTagThree>
              </BaseTagThree>
            </ChildC>
            <ChildC name="ctwo">
              <BaseTagThree>
              </BaseTagThree>
              <BaseTagTarget>
              </BaseTagTarget>
            </ChildC>
            <ChildC name="cthree">
              <BaseTagThree>
              </BaseTagThree>
            </ChildC>
          </ChildB>
        </Root>
        */
        #endregion

        //快速获取
        public static void QuicklyGet()
        {
            XmlFile myXml = new XmlFile("D:\\XML\\", "QuicklyGet.xml");
            //获取子标签
            //GetChildTag
            XmlChildTag childA = myXml.GetChildTag("Root", "ChildA");
            childA.Attrs.Add("changed", "ture");
            //获取基础标签
            //GetBaseTag
            XmlBaseTag baseTagTwo = myXml.GetBaseTag("Root", "ChildA", "BaseTagTwo");
            baseTagTwo.InnerText = "默认返回第一个被找到的标签/default return first tag be found";
            //在子标签内继续快速获取标签
            //quickly get tag from a XmlChildTag
            XmlChildTag childB = myXml.GetChildTag("Root", "ChildB");
            XmlBaseTag target = childB.ChildTagList.Where(c => c.Attrs["name"] == "ctwo").First().GetBaseTag("BaseTagTarget");
            target.InnerText = "快速获取和lambda组合/quickly get combine with lambda";
            myXml.Save();
        }
    }

sixants.xmlfile's People

Contributors

lin2code avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.