NET调用XML(使用动态对象)

<?xml version="1.0" encoding="utf-8" ?>
<Scenes>
  <AdviceArea description="常用意见的配置区域">
    <Scene nodeId="1" description="审批节点ID为1的审批人常用意见配置">
      <Advice content="aaaaaaaaaaaaaaaaaaaaaaa"/>
      <Advice content="bbbbbbbbbbbbbbbbbbbbbbb"/>
      <Advice content="ccccccccccccccccccccccc"/>
    </Scene>
    <Scene nodeId="2" description="审批节点ID为2的审批人常用意见配置">
      <Advice content="ddddddddddddddddddddddd"/>
      <Advice content="eeeeeeeeeeeeeeeeeeeeeee"/>
      <Advice content="fffffffffffffffffffffff"/>
    </Scene>
  </AdviceArea>
</Scenes>

  //常用意见的场景文件地址
            string scenePath =Request.PhysicalPath.Replace("Approval.aspx", "Scene.xml");
            if(File.Exists(scenePath))
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(scenePath);
                //加载Xml文件 
                dynamic sceneXML=XmlUtil.ToObject(doc.InnerXml);
                //意见列表集合
                List<DynamicXElement> sceneXMLList = null;
                bool collections = sceneXML.AdviceArea.Scene is IEnumerable<dynamic>;
                if (collections)
                {
                    sceneXMLList = sceneXML.AdviceArea.Scene;
                }
                else
                {
                    sceneXMLList = new List<DynamicXElement>();
                    sceneXMLList.Add(sceneXML.AdviceArea.Scene);
                }
                foreach (dynamic xml in sceneXMLList)
                {
                    string nodeId = (string)xml.XContent.FirstAttribute;
                }
            }




using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace Zemt.Process.Util
{
    public class XmlUtil
    {
        public static string ToXml(dynamic dynamicObject)
        {
            DynamicXElement xmlNode = dynamicObject;
            return xmlNode.XContent.ToString();
        }
        public static dynamic ToObject(string xml, dynamic dynamicResult)
        {
            XElement element = XElement.Parse(xml);
            dynamicResult = new DynamicXElement(element);
            return dynamicResult;
        }
        public static dynamic ToObject(string xml)
        {
            XElement element = XElement.Parse(xml);
            dynamic dynamicResult = new DynamicXElement(element);
            return dynamicResult;
        }
    }

    public class DynamicXElement : DynamicObject
    {
        public DynamicXElement(XElement node)
        {
            this.XContent = node;
        }
        public DynamicXElement()
        {
        }
        public DynamicXElement(String name)
        {
            this.XContent = new XElement(name);
        }
        public XElement XContent
        {
            get;
            private set;
        }
        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            XElement setNode = this.XContent.Element(binder.Name);
            if (setNode != null)
                setNode.SetValue(value);
            else
            {
                //creates an XElement without a value.
                if (value.GetType() == typeof(DynamicXElement))
                    this.XContent.Add(new XElement(binder.Name));
                else
                    this.XContent.Add(new XElement(binder.Name, value));
            }
            return true;
        }
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            XElement getNode = this.XContent.Element(binder.Name);
            IList<XElement> nodes = this.XContent.Elements(binder.Name).ToList<XElement>();
            if (nodes.Count == 1)
            {
                result = new DynamicXElement(getNode);
            }
            else if (nodes.Count > 1)
            {
                IList<DynamicXElement> des = new List<DynamicXElement>();
                foreach (var e in nodes)
                {
                    des.Add(new DynamicXElement(e));
                }
                result = des;
            }
            else
            {
                result = new DynamicXElement(binder.Name);
            }
            return true;
        }
        public override bool TryConvert(ConvertBinder binder, out object result)
        {
            if (binder.Type == typeof(String))
            {
                result = this.XContent.Value;
                return true;
            }
            else
            {
                result = null;
                return false;
            }
        }
    }
}

 

发布了230 篇原创文章 · 获赞 184 · 访问量 35万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览