Beispiel #1
0
        public XmlDocument ConvertObjectToXml(XmlObjectNode objectNode)
        {
            XmlDocument xml = new XmlDocument();

            xml.AppendChild(xml.CreateXmlDeclaration("1.0", "utf-8", null));
            var rootXml = xml.CreateElement(objectNode.Name);

            if (objectNode.Propertes.Count > 0)
            {
                foreach (var item in objectNode.Propertes)
                {
                    rootXml.SetAttribute(item.Key, item.Value);
                }
            }
            if (objectNode.HasChild)
            {
                CreateChildNodes(objectNode.Value as List <XmlObjectNode>, xml, rootXml);
            }
            else
            {
                var textNode = xml.CreateTextNode(objectNode.Name);
                textNode.InnerText = objectNode.Value.ToString();
                rootXml.AppendChild(textNode);
            }
            xml.AppendChild(rootXml);
            return(xml);
        }
Beispiel #2
0
        public XmlObjectNode ConvertXmlToObject(string xmlContent)
        {
            var xml = new XmlDocument();

            xml.LoadXml(xmlContent);

            var           root     = xml.ChildNodes[1] as XmlElement;
            XmlObjectNode rootNode = new XmlObjectNode();

            rootNode.Name     = root.Name;
            rootNode.HasChild = root.HasChildNodes;
            if (root.HasChildNodes)
            {
                rootNode.Value = GetChildNodes(root);
            }
            else
            {
                rootNode.Value = "xxx";
            }
            if (root.HasAttributes)
            {
                foreach (XmlAttribute item in root.Attributes)
                {
                    rootNode.Propertes.Add(item.Name, item.Value);
                }
            }
            return(rootNode);
        }
Beispiel #3
0
        /// <summary>
        /// 获取当前节点子节点
        /// </summary>
        /// <param name="curruntNode">当前节点</param>
        /// <returns>返回子节点集体</returns>
        private static object GetChildNodes(XmlElement curruntNode)
        {
            List <XmlObjectNode> nodes = new List <XmlObjectNode>();

            foreach (var itemNode in curruntNode.ChildNodes)
            {
                XmlObjectNode rootNode = new XmlObjectNode();
                if (itemNode is XmlElement)
                {
                    var xmlNode = itemNode as XmlElement;
                    rootNode.Name     = xmlNode.Name;
                    rootNode.HasChild = xmlNode.HasChildNodes;

                    if (xmlNode.HasAttributes)
                    {
                        foreach (XmlAttribute item in xmlNode.Attributes)
                        {
                            rootNode.Propertes.Add(item.Name, item.Value);
                        }
                    }
                    if (xmlNode.HasChildNodes)
                    {
                        rootNode.Value = GetChildNodes(xmlNode);
                    }
                    else
                    {
                        rootNode.Value = xmlNode.InnerText;
                    }
                }
                else if (itemNode is XmlText)
                {
                    var xmlNode = itemNode as XmlText;
                    return(xmlNode.InnerText);
                }
                nodes.Add(rootNode);
            }
            return(nodes);
        }