17 public class XmlElement
19 private readonly
bool hasPrefix;
20 private bool hasNameSpaces;
21 private bool hasDefaultNameSpace;
22 private bool hasAttributes;
23 private bool hasInnerValue;
24 private bool hasChildren;
25 private string innerValue;
26 private string defaultXmlNsUri;
35 public string Name {
get;
private set; }
39 public List<XmlElement>
Children {
get;
private set; }
43 public HashSet<XmlAttribute>
Attributes {
get;
private set; }
57 if (
string.IsNullOrEmpty(value))
60 hasInnerValue =
false;
75 internal XmlElement(
string name,
string prefix)
79 this.hasPrefix = !
string.IsNullOrEmpty(prefix);
89 internal void AddNameSpaceAttribute(
string prefix,
string rootNameSpace,
string uri)
91 if (
string.IsNullOrEmpty(prefix) ||
string.IsNullOrEmpty(uri))
103 hasNameSpaces =
true;
104 AddAttribute(prefix, uri, rootNameSpace);
111 internal void AddDefaultXmlNameSpace(
string defaultXmlNsUri)
113 this.defaultXmlNsUri = defaultXmlNsUri;
114 hasDefaultNameSpace =
true;
123 internal void AddAttribute(
string name,
string value,
string prefix =
"")
128 hasAttributes =
true;
130 Attributes.Add(XmlAttribute.CreateAttribute(name, value, prefix));
137 internal void AddAttribute(XmlAttribute? nullableAttribute)
139 if (!nullableAttribute.HasValue)
146 hasAttributes =
true;
155 internal void AddAttributes(IEnumerable<XmlAttribute> attributes)
157 if (attributes ==
null || !attributes.Any())
164 hasAttributes =
true;
166 foreach (XmlAttribute attribute
in attributes)
181 internal XmlElement AddChildElementWithAttribute(
string name,
string attributeName,
string attributeValue,
string namePrefix =
"",
string attributePrefix =
"")
183 XmlElement childElement = CreateElementWithAttribute(name, attributeName, attributeValue, namePrefix, attributePrefix);
184 AddChildElement(childElement);
195 internal XmlElement AddChildElementWithValue(
string name,
string innerValue,
string prefix =
"")
197 if (
string.IsNullOrEmpty(innerValue))
201 XmlElement childElement = CreateElement(name, prefix);
202 childElement.InnerValue = innerValue;
203 AddChildElement(childElement);
213 internal XmlElement AddChildElement(
string name,
string prefix =
"")
215 XmlElement childElement = CreateElement(name, prefix);
216 AddChildElement(childElement);
224 internal void AddChildElement(XmlElement xmlElement)
226 if (xmlElement ==
null)
242 internal void AddChildElements(IEnumerable<XmlElement> xmlElements)
244 if (xmlElements ==
null || !xmlElements.Any())
262 XmlDocument doc =
new XmlDocument
266 XmlNamespaceManager nsManager =
new XmlNamespaceManager(doc.NameTable);
271 if (nameSpace.Key ==
"xmlns")
275 nsManager.AddNamespace(nameSpace.Key, nameSpace.Value);
279 System.Xml.XmlElement rootElement =
null;
280 if (hasDefaultNameSpace)
282 rootElement = XmlElement.CreateXmlElement(doc,
this, nsManager, defaultXmlNsUri);
286 rootElement = XmlElement.CreateXmlElement(doc,
this, nsManager);
288 doc.AppendChild(rootElement);
301 return Enumerable.Empty<XmlElement>();
303 List<XmlElement> result =
new List<XmlElement>();
304 foreach (XmlElement child
in Children)
306 if (child.
Name == name)
352 List<XmlElement> result =
new List<XmlElement>();
353 foreach (XmlElement child
in Children)
355 if (child.Name == elementName && child.hasAttributes)
357 XmlAttribute? attribute = XmlAttribute.FindAttribute(attributeName, child.Attributes);
358 if (attribute !=
null)
360 if (!useValue || (useValue && attribute.Value.Value == attributeValue))
366 result.AddRange(child.FindChildElementsByNameAndAttribute(elementName, attributeName, attributeValue, useValue));
377 internal static XmlElement CreateElement(
string name,
string prefix =
"")
379 return new XmlElement(name, prefix);
391 internal static XmlElement CreateElementWithAttribute(
string name,
string attributeName,
string attributeValue,
string namePrefix =
"",
string attributePrefix =
"")
393 XmlElement element =
new XmlElement(name, namePrefix)
397 element.Attributes.Add(XmlAttribute.CreateAttribute(attributeName, attributeValue, attributePrefix));
398 element.hasAttributes =
true;
410 private static System.Xml.XmlElement CreateXmlElement(XmlDocument doc, XmlElement customElement, XmlNamespaceManager nsManager,
string defaultXmlNsUri =
null)
412 System.Xml.XmlElement xmlElem;
413 if (customElement.hasPrefix)
415 xmlElem = doc.CreateElement(customElement.Prefix, customElement.Name, nsManager.LookupNamespace(customElement.Prefix));
419 xmlElem = doc.CreateElement(customElement.Name, defaultXmlNsUri);
423 if (customElement.hasAttributes)
425 foreach (var attr
in customElement.Attributes)
429 System.Xml.XmlAttribute xmlAttr = doc.CreateAttribute(attr.Prefix, attr.Name, nsManager.LookupNamespace(attr.Prefix));
430 xmlAttr.Value = attr.Value;
431 xmlElem.Attributes.Append(xmlAttr);
435 xmlElem.SetAttribute(attr.Name, attr.Value);
441 if (customElement.hasInnerValue)
443 xmlElem.InnerText = customElement.InnerValue;
447 if (customElement.hasChildren)
449 foreach (var child
in customElement.Children)
451 System.Xml.XmlElement childXmlElem = XmlElement.CreateXmlElement(doc, child, nsManager, defaultXmlNsUri);
452 xmlElem.AppendChild(childXmlElem);