Add or delete Xml subnodes in C,
// Add an xml Node
Private void AddXml (string image, string title)
{
XmlDocument xmlDoc = new XmlDocument ();
XmlDoc. Load (Server. MapPath ("../flash/dati. xml "));
XmlNode root = xmlDoc. SelectSingleNode ("images"); // query <images>
XmlElement xe1 = xmlDoc. CreateElement ("thumb"); // create a <thumb> node
Xe1.SetAttribute ("displayNum", "6"); // you can specify the displayNum attribute of a node.
Xe1.SetAttribute ("separation", "5"); // set the separation attribute of the node XmlElement xesub1 = xmlDoc. CreateElement ("image ");
Xesub1.InnerText = image; // sets the text node.
Xe1.AppendChild (xesub1); // Add it to the thumb Node
XmlElement xesub2 = xmlDoc. CreateElement ("description ");
Xesub2.InnerText = title;
Xe1.AppendChild (xesub2); root. AppendChild (xe1); // Add it to the <images> node.
XmlDoc. Save (Server. MapPath ("../flash/dati. xml "));
} Public void WriteXml (string FileName, string name, string age, string sex)
{
// Initialize the XML document operation class
XmlDocument myXml = new XmlDocument ();
// Load the specified XML file
MyXml. Load (FileName );
// Add element-name
XmlElement ele0 = myXml. CreateElement ("name ");
XmlText Text0 = myXml. CreateTextNode (name );
// Add element-age
XmlElement ele1 = myXml. CreateElement ("age ");
XmlText Text1 = myXml. CreateTextNode (age );
// Add element-Gender
XmlElement ele2 = myXml. CreateElement ("sex ");
XmlText Text2 = myXml. CreateTextNode (sex );
// Node for adding elements --- studentRecord
XmlNode newElem = myXml. CreateNode ("element", "studentRecord ","");
// Add an element to a node
NewElem. AppendChild (ele0 );
NewElem. LastChild. AppendChild (Text0 );
NewElem. AppendChild (ele1 );
NewElem. LastChild. AppendChild (Text1 );
NewElem. AppendChild (ele2 );
NewElem. LastChild. AppendChild (Text2 );
// Add the node to the document
XmlElement root = myXml. DocumentElement;
Root. AppendChild (newElem );
// Save all changes
MyXml. Save (FileName );
}
// Delete node content
Private void DelXml (string image)
{
XmlDocument xmlDoc = new XmlDocument ();
XmlDoc. Load (Server. MapPath ("../flash/dati. xml "));
XmlNodeList xnl = xmlDoc. SelectSingleNode ("images"). ChildNodes;
Foreach (XmlNode xn in xnl)
{
XmlElement xe = (XmlElement) xn;
If (xe. InnerText. IndexOf (image, 0)> = 0)
{
Xn. ParentNode. RemoveChild (xn );
// Xn. RemoveAll ();
}
}
XmlDoc. Save (Server. MapPath ("../flash/dati. xml "));
} // Modify foreach (XmlNode xn in nodeList) // traverse all subnodes
{
XmlElement xe = (XmlElement) xn; // converts the subnode type to the XmlElement type
If (xe. GetAttribute ("genre") = "") // interpretation Condition
{
Xe. SetAttribute ("genre", newStr); // modify this attribute to newstr.
XmlNodeList nls = xe. ChildNodes; // continue to obtain all the child nodes of the xe subnode
Foreach (XmlNode xn1 in nls) // traverse
{
XmlElement xe2 = (XmlElement) xn1; // Conversion Type
If (xe2.Name = "author") // if conditions are found //
{
Xe2.InnerText = newText; // modify
Break; // find and exit.
}
}
Break;
} // Write XML
Private void WriteXML ()
{
XmlTextWriter xtw = new XmlTextWriter (@ "MessageBook \ Data \ MessageBook1.xml"), null );
Xtw. Formatting = Formatting. Indented;
Xtw. WriteStartDocument ();
// Xtw. WriteComment ();
Xtw. WriteStartElement ("Item ");
Xtw. WriteElementString ("UserName", "Tom ");
Xtw. WriteEndElement ();
Xtw. WriteEndDocument ();
Xtw. Close ();
}
// Read XML
Private void ReadXML ()
{
// Create an XmlTextReader Class Object and call the Read method to Read the XML file
XmlTextReader textReader = new XmlTextReader ("C: \ books. xml ");
TextReader. Read ();
// Execute the loop body if the node is not empty
While (textReader. Read ())
{
// Read the first element
TextReader. MoveToElement ();
Console. WriteLine ("XmlTextReader Properties Test ");
Console. WriteLine ("========================= ");
// Read the attributes of the element and display it on the console
Console. WriteLine ("Name:" + textReader. Name );
Console. WriteLine ("Base URI:" + textReader. BaseURI );
Console. WriteLine ("Local Name:" + textReader. LocalName );
Console. WriteLine ("Attribute Count:" + textReader. AttributeCount. ToString ());
Console. WriteLine ("Depth:" + textReader. Depth. ToString ());
Console. WriteLine ("Line Number:" + textReader. LineNumber. ToString ());
Console. WriteLine ("Node Type:" + textReader. NodeType. ToString ());
Console. WriteLine ("Attribute Count:" + textReader. Value. ToString ());
}