Extract String from XmlNode - CSharp System.Xml

CSharp examples for System.Xml:XML Node

Description

Extract String from XmlNode

Demo Code


using System.Diagnostics;
using System.Xml;
using System.Collections.Generic;
using System;/* w  w w. j  a  v a2  s . c  o  m*/

public class Main{
        public static string ExtractString(XmlNode node, string nodeName, string defualtVal, bool isMust)
        {
            string result = defualtVal;

            if (node == null || !node.HasChildNodes || node.SelectSingleNode(nodeName) == null)
            {
                if (isMust)
                {
                    Debug.Assert(false, "ExtactString Error!");
                }

                return result;
            }

            XmlNode childNode = node.SelectSingleNode(nodeName);

            string nodeText = childNode.InnerText;
            if (string.IsNullOrEmpty(nodeText))
            {
                if (isMust)
                {
                    Debug.Assert(false, "ExtactString Error!");
                }
            }
            else
            {
                result = nodeText;
            }

            return result;
        }
}

Related Tutorials