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);
297 internal void WriteTo(XmlWriter writer,
string defaultNs =
null)
299 string elementDefaultNs = hasDefaultNameSpace ? defaultXmlNsUri : defaultNs;
308 writer.WriteStartElement(
Prefix,
Name, nsUri);
312 writer.WriteStartElement(
Name, elementDefaultNs);
319 if (ns.Key ==
"xmlns")
323 writer.WriteAttributeString(
"xmlns", ns.Key,
null, ns.Value);
333 if (attr.Prefix ==
"xmlns")
337 string attrNsUri =
null;
342 writer.WriteAttributeString(attr.Prefix, attr.Name, attrNsUri, attr.Value);
346 int colonIndex = attr.Name.IndexOf(
':');
351 string implicitPrefix = attr.Name.Substring(0, colonIndex);
352 string localName = attr.Name.Substring(colonIndex + 1);
358 writer.WriteAttributeString(implicitPrefix, localName, nsUri, attr.Value);
362 writer.WriteAttributeString(attr.Name, attr.Value);
370 writer.WriteString(innerValue);
375 foreach (XmlElement child
in Children)
377 child.WriteTo(writer, elementDefaultNs);
381 writer.WriteEndElement();
393 return Enumerable.Empty<XmlElement>();
395 List<XmlElement> result =
new List<XmlElement>();
396 foreach (XmlElement child
in Children)
398 if (child.
Name == name)
444 List<XmlElement> result =
new List<XmlElement>();
445 foreach (XmlElement child
in Children)
447 if (child.Name == elementName && child.hasAttributes)
449 XmlAttribute? attribute = XmlAttribute.FindAttribute(attributeName, child.Attributes);
450 if (attribute !=
null)
452 if (!useValue || (useValue && attribute.Value.Value == attributeValue))
458 result.AddRange(child.FindChildElementsByNameAndAttribute(elementName, attributeName, attributeValue, useValue));
469 internal static XmlElement CreateElement(
string name,
string prefix =
"")
471 return new XmlElement(name, prefix);
483 internal static XmlElement CreateElementWithAttribute(
string name,
string attributeName,
string attributeValue,
string namePrefix =
"",
string attributePrefix =
"")
485 XmlElement element =
new XmlElement(name, namePrefix)
489 element.Attributes.Add(XmlAttribute.CreateAttribute(attributeName, attributeValue, attributePrefix));
490 element.hasAttributes =
true;
502 private static System.Xml.XmlElement CreateXmlElement(XmlDocument doc, XmlElement customElement, XmlNamespaceManager nsManager,
string defaultXmlNsUri =
null)
504 System.Xml.XmlElement xmlElem;
505 if (customElement.hasPrefix)
507 xmlElem = doc.CreateElement(customElement.Prefix, customElement.Name, nsManager.LookupNamespace(customElement.Prefix));
511 xmlElem = doc.CreateElement(customElement.Name, defaultXmlNsUri);
515 if (customElement.hasAttributes)
517 foreach (var attr
in customElement.Attributes)
521 System.Xml.XmlAttribute xmlAttr = doc.CreateAttribute(attr.Prefix, attr.Name, nsManager.LookupNamespace(attr.Prefix));
522 xmlAttr.Value = attr.Value;
523 xmlElem.Attributes.Append(xmlAttr);
527 xmlElem.SetAttribute(attr.Name, attr.Value);
533 if (customElement.hasInnerValue)
535 xmlElem.InnerText = customElement.InnerValue;
539 if (customElement.hasChildren)
541 foreach (var child
in customElement.Children)
543 System.Xml.XmlElement childXmlElem = XmlElement.CreateXmlElement(doc, child, nsManager, defaultXmlNsUri);
544 xmlElem.AppendChild(childXmlElem);