Thứ Năm, 13 tháng 9, 2007

Quick Xml Read and Write Adapters for C#

#region XmlWriterAdapter and XmlReaderAdapter

#region XmlReaderAdapter
///
/// Allows Easy Reading from XmlNodes
///

public class XmlReaderAdapter {
#region Attributes
private XmlNode currNode;
#endregion

#region Constructors
public XmlReaderAdapter(XmlNode currNode){
this.currNode = currNode;
}
public XmlReaderAdapter(XmlDocument doc):this(doc.FirstChild){
}
#endregion

#region Getting text and attribute values
///
/// Get child value of a node.
///

public string this[string child]{
get{
return currNode.SelectSingleNode("child::" + child).InnerText;
}
}
///
/// Get child attribute.
///

public string this[string child, string attribute]{
get{
return currNode.SelectSingleNode("child::" + child).Attributes[attribute].Value;
}
}
#endregion

#region Check if child exists
///
/// Return whether child value exists.
///

///
///
public bool hasChildValue(string child){
return (hasChild(child) && (this[child].Length>0));
}
public bool hasChild(string child){
return (this.currNode.SelectSingleNode("child::"+child)!=null);
}
#endregion

#region Get Sub-Nodes - Old version
///
/// Get Child node collection from a wrapper name - old version
///

///
///
public XmlNodeList getSubNodeCollection(string collectionWrapperChild){
return currNode.SelectSingleNode("child::" + collectionWrapperChild).ChildNodes;
}
#endregion

#region Get SubNode / Children Adapter Collections - new version
///
/// Get Child node collection from a wrapper name
///

///
///
public XmlReaderAdapterCollection getSubNodeAdapterCollection(string collectionWrapperChild){
return new XmlReaderAdapterCollection(currNode.SelectSingleNode("child::" + collectionWrapperChild).ChildNodes);
}
///
/// Get All Child nodes
///

///
///
public XmlReaderAdapterCollection getAllChildren(){
return new XmlReaderAdapterCollection(currNode.ChildNodes);
}

#endregion
}
#endregion

#region XmlReaderAdapterCollection
///
/// Provides a strongly typed collection of XmlReaderAdapter objects.
///

[Serializable()]
public class XmlReaderAdapterCollection : CollectionBase{
///
/// Creates Default XmlReaderAdapterCollection
///

public XmlReaderAdapterCollection(){
}
///
/// Creates XmlReaderAdapterCollection from a list of nodes
///

public XmlReaderAdapterCollection(XmlNodeList nodes){
foreach (XmlNode node in nodes){
this.Add(new XmlReaderAdapter(node));
}
}///
/// Returns a XmlReaderAdapter object from the specified ordinal position within the collection.
///

public XmlReaderAdapter this[int index]{
get{
return (XmlReaderAdapter)List[index];
}
set{
List[index] = value;
}
}
///
/// Adds a VersionCriteria to the VersionCriteriaCollection.
///

/// The XmlReaderAdapter object to add to the collection.
/// The position within the collection that the XmlReaderAdapter object was added.
public int Add(XmlReaderAdapter value){
return(List.Add(value));
}

///
/// Determines the index in the XmlReaderAdapterCollection of the XmlReaderAdapter object specified.
///

/// The VersionCriteria object to locate within the collection.
/// The index of the VersionCriteria object; if not found returns -1.
public int IndexOf(XmlReaderAdapter value){
return(List.IndexOf(value));
}

///
/// Adds the VersionCriteria object at the specified ordinal position in the collection.
///

/// The zero based index at which the XmlReaderAdapter object should be added.
/// The XmlReaderAdapter to be added to the XmlReaderAdapterCollection.
public void Insert(int index, XmlReaderAdapter value){
List.Insert(index, value);
}

///
/// Removes the XmlReaderAdapter object from the XmlReaderAdapterCollection.
///

/// The XmlReaderAdapter object to remove.
public void Remove(XmlReaderAdapter value){
List.Remove(value);
}

///
/// Determines if the XmlReaderAdapterCollection contains the specified XmlReaderAdapter object.
///

/// The XmlReaderAdapter to search for.
/// true if the XmlReaderAdapter object is found in the collection; false if not.
public bool Contains(XmlReaderAdapter value){
return(List.Contains(value));
}
}
#endregion

#region XmlWriterAdapter
///
/// Allows Easy Writing of an XmlDocument
///

public class XmlWriterAdapter{
#region Properties and Attributes
private XmlDocument doc;
private string docNamespace;
private XmlNode rootNode;
///
/// Allows you to retrieve the RootNode, so you can append to the root if needed
///

public XmlNode RootNode{
get{
return this.rootNode;
}
}
///
/// Allows you to retrieve the XmlDocument in case you need to do something not
/// defined here.
///

public XmlDocument Doc{
get{
return this.doc;
}
}
#endregion

#region Constructors
///
/// Creates a XmlWriterAdapter with no namespace (THIS IS NORMAL)
///

///
public XmlWriterAdapter(string rootNodeName):this(string.Empty,rootNodeName){
}
///
/// Creates a XmlWriterAdapter with a namespace (CURRENTLY NOT USED)
///

///
///
public XmlWriterAdapter(string docNamespace, string rootNodeName){
this.docNamespace = docNamespace;
this.doc = new XmlDocument();
rootNode = MakeAndSetElement(rootNodeName);
doc.AppendChild(rootNode);
}
#endregion

#region Creating Elements with inner text
///
/// Creates and returns an element with the given name and NO value.
///

///
///
public XmlNode MakeAndSetElement(string name){
return doc.CreateNode(XmlNodeType.Element, name, docNamespace);
}
///
/// Creates and returns an element with the given name and value
///

///
///
///
public XmlNode MakeAndSetElement(string name, string value){
XmlNode field = MakeAndSetElement(name);
//if (value != string.Empty){
field.InnerText = value;
//}
return field;
}
#endregion

#region Add Attribute
///
/// Makes and sets an attribute.
///

///
///
///
public void MakeAndSetAttribute(string name, string value, XmlNode parent){
XmlAttribute field = doc.CreateAttribute(name, docNamespace);
field.Value = value;
parent.Attributes.Append(field);
}
#endregion

#region Append Element
///
/// Creates an element with the given name and value. Appends Element to the given parent.
///

///
///
///
///
public XmlNode AppendElement(string name, string value, XmlNode parent){
return AppendElement(MakeAndSetElement(name,value),parent);
}
///
/// Creates an element with the given name and NO value. Appends Element to the given parent.
///

///
///
///
public XmlNode AppendElement(string name, XmlNode parent){
return AppendElement(MakeAndSetElement(name),parent);
}
///
/// Appends an element to the given parent.
///

///
///
///
public XmlNode AppendElement(XmlNode child, XmlNode parent){
parent.AppendChild(child);
return child;
}
#endregion

#region AppendElementToRoot
///
/// Creates an element with the given name and NO value. Appends Element to the root.
///

///
///
public XmlNode AppendElementToRoot(string name){
return AppendElementToRoot(name,string.Empty);
}
///
/// Creates an element with the given name and value. Appends Element to the root.
///

///
///
public XmlNode AppendElementToRoot(string name, string value){
return AppendElementToRoot(MakeAndSetElement(name,value));
}
///
/// Appends an element to the root.
///

///
///
///
public XmlNode AppendElementToRoot(XmlNode child){
return AppendElement(child,this.rootNode);
}
#endregion

#region Write XML
///
/// Writes XML to string in Indented Format (good for debugging or printing to user)
///

///
public string WriteIndentedXML(){
StringWriter strWriter = new StringWriter();
XmlTextWriter xmlWriter = new XmlTextWriter(strWriter);
xmlWriter.Formatting = Formatting.Indented;
doc.WriteTo(xmlWriter);
xmlWriter.Flush();
strWriter.Flush();
return strWriter.ToString();
}
///
/// Writes XML to string with no formatting (good for sending it to other programs)
///

///
public string WriteXML(){
//System.Diagnostics.Trace.WriteLine(WriteIndentedXML());

StringWriter strWriter = new StringWriter();
XmlTextWriter xmlWriter = new XmlTextWriter(strWriter);
xmlWriter.Formatting = Formatting.None;
doc.WriteTo(xmlWriter);
xmlWriter.Flush();
strWriter.Flush();
return strWriter.ToString();
}
#endregion
}
#endregion

#endregion


ColinBashBash

1 nhận xét:

pretty girl nói...

Chào bạn.
Mình đang cần tìm tài liệu để đọc và ghi file XML. Mình thấy phần "Quick Xml Read and Write Adapters for C#" khá hay nhưng không hiểu lắm. Bạn có thể gửi cho mình một ứng dụng đã làm sử dụng các Class này được hay không vào địa chỉ của mình dungnd08@gmail.com. Thanks