[C#] XML 문서 노드 추가 함수

C# XML 노드 추가 함수

/// <summary>
/// XML 문서에 노드 추가
/// </summary>
/// <param name="sNodeName">추가할 Node 명</param>
/// <param name="oParent">부모 객체</param>
/// <param name="xDoc">xml 문서</param>
/// <param name="sValue">Node 값</param>
/// <returns>반환할 XML Node</returns>
public XmlElement MakeXmlElement(string sNodeName, object oParent, ref XmlDocument xDoc, string sValue)
{
    XmlElement xlTemp;
    string[] sNodeValue = sNodeName.Split(':');

    if (sNodeValue.Length == 2)
        xlTemp = xDoc.CreateElement(sNodeValue[0], sNodeValue[1], "");
    else
        xlTemp = xDoc.CreateElement(sNodeName);

    if (sValue.Trim() != "") xlTemp.InnerText = sValue;

    try
    {
        if (oParent != null)
        {
            Type t = oParent.GetType();
            if (t.ToString().Equals("System.Xml.XmlElement"))
            {
                XmlElement xlParent = (XmlElement)oParent;
                xlParent.AppendChild(xlTemp);
            }
            else if (t.ToString().Equals("System.Xml.XmlDocument"))
            {
                xDoc.DocumentElement.AppendChild(xlTemp);
            }
        }

        return xlTemp;
    }
    catch (Exception)
    {
    }

    return null;
}

댓글 남기기