using System; using System.Xml; public class SelectNodesByNamespace { private static void Main() { XmlDocument doc = new XmlDocument(); doc.Load("Order.xml"); XmlNodeList matches = doc.GetElementsByTagName("*","http://mycompany/OrderML"); foreach (XmlNode node in matches) { Console.Write(node.Name + " "); foreach (XmlAttribute attribute in node.Attributes) { Console.Write(attribute.Value + " "); } } } }
Author: coder
Set Xml Node Value
using System; using System.Text; using System.Xml; //GNU General Public License version 2 (GPLv2) //http://cbasetest.codeplex.com/license public class MainClass{ public static void SetXmlNodeValue(XmlNode xmlae, object value) { SetXmlNodeValue(xmlae, value, string.Empty); } public static void SetXmlNodeValue(XmlNode xmlae, string value) { XmlDocument ownerDocument = xmlae.OwnerDocument; foreach (XmlNode node in xmlae.ChildNodes) { if (node.NodeType == XmlNodeType.Text) { node.Value = value; } } } public static void SetXmlNodeValue(XmlNode xmlae, object value, string defaultStr) { string str = defaultStr; if (value != null) { str = value.ToString(); } if (str != null) { SetXmlNodeValue(xmlae, str.ToString()); } } }
Get attribute from XmlNode
//Microsoft Public License (Ms-PL) //http://dbmlmanager.codeplex.com/license #region using using System; using System.Xml; #endregion namespace DbmlManager.Lib.Utility { #region Class Docs /// <summary> /// Summary description for XmlUtil. /// </summary> #endregion public class XmlUtil { #region GetAttrib(XmlNode node, string name) public static string GetAttrib(XmlNode node, string name) { if (node.Attributes == null) return null; if (node.Attributes[name] == null) return null; return node.Attributes[name].InnerText; } #endregion } }
Get integer value from xml element
//Microsoft Public License (Ms-PL) //http://dbmlmanager.codeplex.com/license #region using using System; using System.Xml; #endregion namespace DbmlManager.Lib.Utility { #region Class Docs /// <summary> /// Summary description for XmlUtil. /// </summary> #endregion public class XmlUtil { #region LoadInt(ref int val, XmlNode parent, string nodeName) public static void LoadInt(ref int val, XmlNode parent, string nodeName) { try { XmlElement elem = parent[nodeName]; if (elem != null) val = Int32.Parse(elem.InnerText.Trim()); } catch { val = 0; } } #endregion } }
Load boolean value from xml element
//Microsoft Public License (Ms-PL) //http://dbmlmanager.codeplex.com/license #region using using System; using System.Xml; #endregion namespace DbmlManager.Lib.Utility { #region Class Docs /// <summary> /// Summary description for XmlUtil. /// </summary> #endregion public class XmlUtil { #region LoadBool(ref bool val, XmlNode parent, string nodeName) public static void LoadBool(ref bool val, XmlNode parent, string nodeName) { try { XmlElement elem = parent[nodeName]; if (elem != null) val = (elem.InnerText.Trim() == "1"); } catch { val = false; } } #endregion } }
Load String from xml element
//Microsoft Public License (Ms-PL) //http://dbmlmanager.codeplex.com/license #region using using System; using System.Xml; #endregion namespace DbmlManager.Lib.Utility { #region Class Docs /// <summary> /// Summary description for XmlUtil. /// </summary> #endregion public class XmlUtil { #region LoadString(ref string val, XmlNode parent, string nodeName) public static void LoadString(ref string val, XmlNode parent, string nodeName) { try { XmlElement elem = parent[nodeName]; if (elem != null) val = elem.InnerText.Trim(); } catch { val = string.Empty; } } #endregion } }
Create Xml Document, Node
//GNU General Public License version 2 (GPLv2) //http://cbasetest.codeplex.com/license namespace SDFL.Helper { using System; using System.Text; using System.Xml; public class XMLHelper { public static XmlDocument CreateXmlDocument() { return CreateXmlDocument(new Version(1, 0), Encoding.UTF8); } public static XmlDocument CreateXmlDocument(Version version, Encoding encoding) { XmlDocument document = new XmlDocument(); document.AppendChild(document.CreateXmlDeclaration(version.ToString(), encoding.BodyName, string.Empty)); return document; } public static XmlNode CreateXmlNode(XmlDocument xmlDocument, string name) { return CreateXmlNode(xmlDocument, name, string.Empty); } public static XmlNode CreateXmlNode(XmlDocument xmlDocument, string name, string value) { XmlNode node = xmlDocument.CreateNode(XmlNodeType.Element, name, string.Empty); node.AppendChild(xmlDocument.CreateTextNode(value)); return node; } } }