Skip to content

Commit 8ec241c

Browse files
committed
Add XmlDocument methods
1 parent d8d70e6 commit 8ec241c

File tree

1 file changed

+82
-9
lines changed

1 file changed

+82
-9
lines changed

SharpHelpers/SharpHelpers/XmlDocumentHelper.cs

Lines changed: 82 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// (c) 2019 SharpCoding
22
// This code is licensed under MIT license (see LICENSE.txt for details)
33
using System;
4+
using System.IO;
45
using System.Text;
56
using System.Xml;
67
using System.Xml.Linq;
@@ -10,10 +11,11 @@ namespace SharpCoding.SharpHelpers
1011
public static class XmlDocumentHelper
1112
{
1213
/// <summary>
13-
/// Return a XmlDocument from a XDocument
14+
/// Converts an XDocument to an XmlDocument.
1415
/// </summary>
15-
/// <param name="xDocument"></param>
16-
/// <returns></returns>
16+
/// <param name="xDocument">The XDocument to convert.</param>
17+
/// <returns>The converted XmlDocument.</returns>
18+
/// <exception cref="ArgumentNullException">Thrown when xDocument is null.</exception>
1719
public static XmlDocument ToXmlDocument(this XDocument xDocument)
1820
{
1921
if (xDocument == null) throw new ArgumentNullException(nameof(xDocument));
@@ -27,10 +29,11 @@ public static XmlDocument ToXmlDocument(this XDocument xDocument)
2729
}
2830

2931
/// <summary>
30-
/// Return a XDocument from a XmlDocument
32+
/// Converts an XmlDocument to an XDocument.
3133
/// </summary>
32-
/// <param name="xmlDocument"></param>
33-
/// <returns></returns>
34+
/// <param name="xmlDocument">The XmlDocument to convert.</param>
35+
/// <returns>The converted XDocument.</returns>
36+
/// <exception cref="ArgumentNullException">Thrown when xmlDocument is null.</exception>
3437
public static XDocument ToXDocument(this XmlDocument xmlDocument)
3538
{
3639
if (xmlDocument == null) throw new ArgumentNullException(nameof(xmlDocument));
@@ -43,10 +46,11 @@ public static XDocument ToXDocument(this XmlDocument xmlDocument)
4346
}
4447

4548
/// <summary>
46-
/// Return a XmlDocument from a XElement
49+
/// Converts an XElement to an XmlDocument.
4750
/// </summary>
48-
/// <param name="xElement"></param>
49-
/// <returns></returns>
51+
/// <param name="xElement">The XElement to convert.</param>
52+
/// <returns>The converted XmlDocument.</returns>
53+
/// <exception cref="ArgumentNullException">Thrown when xElement is null.</exception>
5054
public static XmlDocument ToXmlDocument(this XElement xElement)
5155
{
5256
if (xElement == null) throw new ArgumentNullException(nameof(xElement));
@@ -61,5 +65,74 @@ public static XmlDocument ToXmlDocument(this XElement xElement)
6165
doc.LoadXml(sb.ToString());
6266
return doc;
6367
}
68+
69+
/// <summary>
70+
/// Finds the first child node with the specified name.
71+
/// </summary>
72+
/// <param name="xmlDocument">The XmlDocument to search.</param>
73+
/// <param name="nodeName">The name of the node to find.</param>
74+
/// <returns>The first matching XmlNode, or null if not found.</returns>
75+
public static XmlNode FindFirstChild(this XmlDocument xmlDocument, string nodeName)
76+
{
77+
if (xmlDocument == null) throw new ArgumentNullException(nameof(xmlDocument));
78+
if (string.IsNullOrWhiteSpace(nodeName)) throw new ArgumentException("Node name cannot be null or empty.", nameof(nodeName));
79+
80+
return xmlDocument.SelectSingleNode($"//{nodeName}");
81+
}
82+
83+
/// <summary>
84+
/// Adds a new child element with the specified name and value to the given parent node.
85+
/// </summary>
86+
/// <param name="xmlDocument">The XmlDocument to modify.</param>
87+
/// <param name="parentNode">The parent node to which the new element will be added.</param>
88+
/// <param name="elementName">The name of the new element.</param>
89+
/// <param name="elementValue">The value of the new element.</param>
90+
/// <returns>The newly created XmlElement.</returns>
91+
public static XmlElement AddChildElement(this XmlDocument xmlDocument, XmlNode parentNode, string elementName, string elementValue)
92+
{
93+
if (xmlDocument == null) throw new ArgumentNullException(nameof(xmlDocument));
94+
if (parentNode == null) throw new ArgumentNullException(nameof(parentNode));
95+
if (string.IsNullOrWhiteSpace(elementName)) throw new ArgumentException("Element name cannot be null or empty.", nameof(elementName));
96+
97+
var newElement = xmlDocument.CreateElement(elementName);
98+
newElement.InnerText = elementValue;
99+
parentNode.AppendChild(newElement);
100+
return newElement;
101+
}
102+
103+
/// <summary>
104+
/// Converts the XmlDocument to a formatted string.
105+
/// </summary>
106+
/// <param name="xmlDocument">The XmlDocument to convert.</param>
107+
/// <returns>A string representation of the XmlDocument.</returns>
108+
public static string ToFormattedString(this XmlDocument xmlDocument)
109+
{
110+
if (xmlDocument == null) throw new ArgumentNullException(nameof(xmlDocument));
111+
112+
using (var stringWriter = new StringWriter())
113+
{
114+
using (var xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings { Indent = true }))
115+
{
116+
xmlDocument.Save(xmlWriter);
117+
}
118+
return stringWriter.ToString();
119+
}
120+
}
121+
122+
/// <summary>
123+
/// Removes the specified node from the XmlDocument.
124+
/// </summary>
125+
/// <param name="xmlDocument">The XmlDocument to modify.</param>
126+
/// <param name="node">The node to remove.</param>
127+
public static void RemoveNode(this XmlDocument xmlDocument, XmlNode node)
128+
{
129+
if (xmlDocument == null) throw new ArgumentNullException(nameof(xmlDocument));
130+
if (node == null) throw new ArgumentNullException(nameof(node));
131+
132+
if (node.ParentNode != null)
133+
{
134+
node.ParentNode.RemoveChild(node);
135+
}
136+
}
64137
}
65138
}

0 commit comments

Comments
 (0)