📄 node.cs
字号:
using System;
using System.Collections;
using System.Xml;
using System.Xml.XPath;
namespace com.use.wfp.util
{
/// <summary>
/// Node 的摘要说明。
/// </summary>
public interface Node{
IList SelectNodes(String xPath);
Node SelectSingleNode(String xPath);
IList GetChildren();
IDictionary GetAttributes();
String GetName();
String GetText();
bool IsEmpty();
}
class EmptyNode:Node
{
public IList SelectNodes(String xPath)
{
return children;
}
public Node SelectSingleNode(String xPath)
{
return this;
}
public IList GetChildren()
{
return children;
}
public IDictionary GetAttributes()
{
return attrs;
}
public String GetName()
{
return "";
}
public String GetText()
{
return "";
}
public bool IsEmpty(){
return true;
}
private IDictionary attrs = new Hashtable();
private IList children = new ArrayList();
}
class NonEmptyNode:Node
{
private IDictionary attrs;
private IList children;
private String text;
private XmlNode xml_node;
public NonEmptyNode(XmlNode xmlNode)
{
this.xml_node = xmlNode;
}
public IList SelectNodes(String xPath)
{
return Convert( xml_node.SelectNodes(xPath) );
}
public Node SelectSingleNode(String xPath)
{
return NodeFactory.CreateNode(xml_node.SelectSingleNode(xPath));
}
public IList GetChildren()
{
if(children == null)
{
children = Convert(xml_node.ChildNodes);
}
return children;
}
public IDictionary GetAttributes()
{
if(attrs == null)
{
attrs = Convert(xml_node.Attributes);
}
return attrs;
}
public String GetName()
{
return xml_node.Name;;
}
public String GetText()
{
if(text == null)
{
text = xml_node.InnerText + "";
}
return text;
}
public bool IsEmpty()
{
return false;
}
private IList Convert(XmlNodeList nodeList)
{
ArrayList retList = new ArrayList();
foreach (XmlNode xmlNode in nodeList)
{
retList.Add(NodeFactory.CreateNode(xmlNode));
}
return retList;
}
private IDictionary Convert(XmlAttributeCollection xmlAttrs)
{
IDictionary attrs = new Hashtable();
foreach (XmlNode xmlNode in xmlAttrs)
{
attrs.Add(xmlNode.Name, xmlNode.Value);
}
return attrs;
}
}
public class NodeFactory
{
private NodeFactory(){}
private static Node empty = new EmptyNode();
public static Node CreateNode(String fileName){
XmlDocument myXmlDocument = new XmlDocument();
myXmlDocument.Load (fileName);
return myXmlDocument.DocumentElement == null ? empty : new NonEmptyNode(myXmlDocument.DocumentElement);
}
public static Node CreateNode(XmlNode xmlNode){
return xmlNode == null ? empty : new NonEmptyNode(xmlNode);
}
public static Node CreateEmptyNode()
{
return empty;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -