- XmlDocument xmlDoc = new XmlDocument();
- string filename = @"c:\books. Xml";
- xmlDoc.Load(filename);
- xmlDoc.Save(Console.Out);
- XmlDocument xmlDoc = new XmlDocument();
- XmlTextReader reader = new XmlTextReader("c:\\books.xml");
- xmlDoc.Load(reader);
- xmlDoc.Save(Console.Out);
- xmlDoc.LoadXml("<Record> write something</ Record>");
- string filename = @"C:\books.xml";
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.Load(filename);
- XmlTextWriter writer = new XmlTextWriter("c:\\domtest.Xml", null);
- writer.Formatting = Formatting.Indented;
- xmlDoc.Save(writer);
- xmlDoc.Save("c:\\domtest. Xml");
- xmlDoc.Save(Console.Out);
- //open an XML file
- string filename = @"c:\books.xml";
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.Load(filename);
- // Create a document fragment.
- XmlDocumentFragment docFrag = xmlDoc.CreateDocumentFragment();
- // Set the contents of the document Fragment.
- docFrag.InnerXml = "<Record> write something</ Record>";
- // Display the document fragment.
- Console.WriteLine(docFrag.InnerXml);
- XmlDocument doc = new XmlDocument();
- doc.LoadXml("<book genre = "programming"> " +
- "<title> ADO.NET programming </ title> " + "</book>");
- // Get the root node
- XmlNode root = doc.DocumentElement;
- // Create a new node.
- XmlElement newbook = doc.CreateElement("price");
- newbook.InnerText = "44.95";
- // Add the node to the document.
- root.AppendChild(newbook);
- doc.Save(Console.Out);

|
METHOD
|
DESCRIPTION
|
|
GetAttribute
|
Returns the attribute value
|
|
HasAttribute
|
Checks if a node has the specified attribute
|
|
RemoveAll
|
Removes all the children and attributes of the current node
|
|
RemoveAllAttributes, RemoveAttribute
|
Removes all attributes and specified attributes from an element respectively
|
|
RemoveAttributeAt
|
Removes the attribute node with the specified index from the attribute collection
|
|
RemoveAttributeNode
|
Removes an XmlAttribute
|
|
SetAttribute
|
Sets the value of the specified attribute
|
|
SetAttribute Node
|
Adds a new xml Attribute
|
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.LoadXml("<Record> some value </Record>");
- // Adding a new comment node to the document
- XmlNode node1 = xmlDoc.CreateComment("DOM Testing sample");
- xmlDoc.AppendChild(node1);
- // Adding a First Name to the documentt
- node1 = xmlDoc.CreateElement("First Name");
- node1.InnerText = "Mahesh";
- xmlDoc.DocumentElement.AppendChild(node1);
- xmlDoc.Save(Console.Out);
- string filename = @"c:\books.xml";
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.Load(filename);
- XmlElement root = xmlDoc.DocumentElement;
- {
- static void Main(string[] args)
- {
- // Load a document fragment
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.LoadXml("<book genre ="programming">" +
- "<title> ADO.NET programming </title> </book>");
- XmlNode root = xmlDoc.DocumentElement;
- Console.WriteLine("XML Document Fragment");
- Console.WriteLine("= = = = = = = = = = = ");
- xmlDoc.Save(Console.Out);
- Console.WriteLine();
- Console.WriteLine("-----------");
- Console.WriteLine("XML Document Fragment Remove All");
- Console.WriteLine("= = = = = = = = = = =");
- // Remove all attributes and child nodes.
- root.RemoveAll();
- // Display the contents on the console after
- // Removing elements and attributes
- xmlDoc.Save(Console.Out);
- }
- }
- public static void Main()
- {
- string filename = "c:\\ books.Xml";
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.Load(filename);
- XmlNode root = xmlDoc.DocumentElement;
- Console.WriteLine("XML Document Fragment");
- Console.WriteLine("= = = = = = = = = = = ");
- xmlDoc.Save(Console.Out);
- Console.WriteLine();
- Console.WriteLine("- - - - - - - - - ");
- Console.WriteLine("XML Document Fragment After RemoveAll");
- Console.WriteLine("= = = = = = = = = = = = ");
- //Remove all attributes and child nodes.
- root.RemoveAll();
- // Display the contents on the console after
- // Removing elements and attributes
- xmlDoc.Save(Console.Out);
- }
- string filename = @"C:\books.xml";
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.Load(filename);
- XmlElement root = xmlDoc.DocumentElement;
- XmlDocumentFragment xmlDocFragment = xmlDoc.CreateDocumentFragment();
- xmlDocFragment.InnerXml =
- "<Fragment><SomeData>Fragment Data</SomeData></ Fragment>";
- XmlElement rootNode = xmlDoc.DocumentElement;
- //Replace xmlDocFragment with rootNode.LastChild
- rootNode.ReplaceChild(xmlDocFragment, rootNode.LastChild);
- xmlDoc.Save(Console.Out);
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.Load(@"C:\\ books.Xml");
- XmlDocumentFragment xmlDocFragment = xmlDoc.CreateDocumentFragment();
- xmlDocFragment.InnerXml =
- "< Fragment >< Some Data> Fragment Data</ Some Data> </ Fragment>";
- XmlNode aNode = xmlDoc.DocumentElement.FirstChild;
- aNode.InsertAfter(xmlDocFragment, aNode.LastChild);
- xmlDoc.Save(Console.Out);
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.Load(@"c:\\books.Xml");
- XmlElement newElem = xmlDoc.CreateElement("NewElement");
- XmlAttribute newAttr = xmlDoc.CreateAttribute("NewAttribute");
- newElem.SetAttributeNode(newAttr);
- // add the new element to the document
- XmlElement root = xmlDoc.DocumentElement;
- root.AppendChild(newElem);
- xmlDoc.Save(Console.Out);
Conclusion
Hope this article would have helped you in Understanding DOM Implementation. See other articles on the website also for further reference.


Join the conversation! Your thoughts help the community grow.