xml Element and Attribute 추가 (Creating New Element and Attribute)


XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlPSW5);

XmlNodeList node = doc.GetElementsByTagName(“ETR_INF”);
for (int i = 0; i < node.Count; i++)
{
//attribute 추가
XmlAttribute newAttribute = doc.CreateAttribute(“LocationName”);
newAttribute.Value = ARL_COD_Name;
node.Item(i).SelectSingleNode(“ARL_COD”).Attributes.Append(newAttribute);

//Element 추가 방법 1
XmlElement newElement = doc.CreateElement(“DEP_DAT”);
XmlText newtext = doc.CreateTextNode(FLI_DAT);
newElement.AppendChild(newtext);
node.Item(i).AppendChild(newElement);

//Element 추가 방법 2
String sGRAND_TOTAL = “<GRAND_TOTAL><GRAND_AMT>” + GRAND_TOTAL + “</GRAND_AMT></GRAND_TOTAL>”;

XmlDocumentFragment xfrag = doc.CreateDocumentFragment();
xfrag.InnerXml = sGRAND_TOTAL;
//조건 Append
if (node.Item(i).SelectNodes(“FAR_INF[FAR_BKD_TYP=’A’]”).Count > 0)
{
node.Item(i).SelectSingleNode(“FAR_INF[FAR_BKD_TYP=’A’]”).AppendChild(xfrag);
}
}

// Element 변경 방법
// Element replace

//Create a new title element.
XmlElement elem = doc.CreateElement(“COS”);
elem.InnerText = ResultSeats;

nodelist.Item(i).ReplaceChild(elem, nodelist.Item(i).SelectSingleNode(“COS”));