zoukankan      html  css  js  c++  java
  • NX二次开发-C#创建XML和解析XML

    在项目中有时因为读写EXCEL速度会慢,不如TXT文本快,而TXT文本有时看上去书写格式又比较乱,

    不能很好的表达树节点,父子节点等需求。这个时候我们可以用XML或者JSON去做了。

    而C#有现成的XML方法使用,用法文档写得还是比较清楚的。微软官方资料https://docs.microsoft.com/zh-cn/dotnet/api/system.xml?view=netcore-3.1

    https://docs.microsoft.com/zh-cn/dotnet/api/system.xml.xmldocument?view=netcore-3.1

    <?xml version="1.0" encoding="utf-8"?>
    <root>
      <student id="20200001">
        <sex>1</sex>
        <name>张三</name>
        <phone>13810012345</phone>
      </student>
      <book>&lt;基础篇&gt;</book>
    </root>

     1.创建XML

    NX11+VS2013
    
    using System;
    using NXOpen;
    using NXOpen.UF;
    using System.Xml;
    
    
    /// <summary>
    /// 创建XML
    /// </summary>
    public static void CreateXml()
    {
        XmlDocument doc = new XmlDocument();
    
        XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
        doc.AppendChild(dec);
    
        //<root>
        XmlElement root = doc.CreateElement("root");
        doc.AppendChild(root);
    
        //<root>/<student>
        XmlElement stu = doc.CreateElement("student");
        root.AppendChild(stu);
    
        //节点属性
        stu.SetAttribute("id", "20200001");
    
        //<root>/<student>/<name>
        XmlElement name = doc.CreateElement("name");
        name.InnerText = "张三";
        stu.AppendChild(name);
    
        //<root>/<student>/<sex>
        XmlElement sex = doc.CreateElement("sex");
        sex.InnerText = "1";
        stu.AppendChild(sex);
    
        stu.AppendChild(name);
    
        //<root>/<student>/<author>
        XmlElement author = doc.CreateElement("phone");
        author.InnerText = "13810012345";
        stu.AppendChild(author);
    
        //特殊字符转义(同HTML)
        XmlElement book = doc.CreateElement("book");
        book.InnerText = "<基础篇>";
        root.AppendChild(book);
    
        //Save方法有多个重载版本
        //1.输出到文件
        doc.Save("D:\新建文件夹\student.xml");
    
        //2.输出为字符串
        string xmlstr = doc.InnerXml;
        theUfSession.Ui.OpenListingWindow();
        theUfSession.Ui.WriteListingWindow("输出XML: " + xmlstr);
        //Console.WriteLine("输出XML: " + xmlstr);
    }
    
    Caesar卢尚宇
    2020年9月29日

     2.解析XML

    NX11+VS2013
    
    using System;
    using NXOpen;
    using NXOpen.UF;
    using System.Xml;
    
    
    
    /// <summary>
    /// 解析XML
    /// </summary>
    public static void ParseXml()
    {
        XmlDataDocument doc = new XmlDataDocument();
    
        //Load(fileName)
        //LoadXml(xmlstr)
        doc.Load("D:\新建文件夹\student.xml");//注意XML文件的位置要搞对
    
        //获取root节点
        XmlElement root = doc.DocumentElement;
    
        //按路径获取一个节点,路径格式path/of/node
        XmlElement student = (XmlElement)root.SelectSingleNode("student");
    
        theUfSession.Ui.OpenListingWindow();
    
        //取出子节点的值
        string name = student["name"].InnerText;
        string sex = student["sex"].InnerText;
        string phone = student["phone"].InnerText;
        int id = Convert.ToInt32(student.GetAttribute("id"));
    
        theUfSession.Ui.WriteListingWindow(name);
        theUfSession.Ui.WriteListingWindow("
    ");
        theUfSession.Ui.WriteListingWindow(sex);
        theUfSession.Ui.WriteListingWindow("
    ");
        theUfSession.Ui.WriteListingWindow(phone);
        theUfSession.Ui.WriteListingWindow("
    ");
        theUfSession.Ui.WriteListingWindow(id.ToString());
        theUfSession.Ui.WriteListingWindow("
    ");
        theUfSession.Ui.WriteListingWindow("
    ");
        theUfSession.Ui.WriteListingWindow("
    ");
    
        //XmlElement node = (XmlElement)doc.SelectSingleNode("root/student/name");
    
        //遍历所有子节点
        foreach (XmlNode node in student.ChildNodes)
        {
            string aa = node.InnerText;
            theUfSession.Ui.WriteListingWindow(aa);
            theUfSession.Ui.WriteListingWindow("
    ");
        }
    }
    
    Caesar卢尚宇
    2020年9月29日

    更多的XML用法,等项目有需求,用到了,在来写例子。

    Caesar卢尚宇

    2020年9月29日

  • 相关阅读:
    两套经典的用户画像-梁宁
    准研一假期的减脂半自律计划
    网络科学导论【第六章】读书脑图
    常见规则网络
    网络科学导论【第五章】读书脑图
    复杂网络链路预测的研究现状及展望
    Python之@property详解及底层实现介绍
    Python触发异常
    一份较认可的文献阅读方案
    HITS算法简介
  • 原文地址:https://www.cnblogs.com/nxopen2018/p/13752213.html
Copyright © 2011-2022 走看看