.NET XML疑问 XmlElement是XmlNode的子类,为什么XmlNode(父类)可以转换成XmlElement(子类)

.NET XML疑问 XmlElement是XmlNode的子类,为什么XmlNode(父类)可以转换成XmlElement(子类),第1张

先贴出来节点关系图

任意子类可以转换为父类

父类不一定可以转换成子类,这里的转化为什么可以,这个取决于xmlnode这个对象本身的节点类型。理解这句话,就发现一点都不奇怪。

一个节点它本身就是Element类型,你只是用Xmlnode来接收赋值,这里已经存在了一个隐式装箱的过程了,然后你再将他转换为它本来的类型,这当然是可以的啦。有什么好疑问的吗?

给你找个类,拿去改改,举一反三就最好了: using System;
using SystemData;
using SystemConfiguration;
using SystemWeb;
using SystemWebSecurity;
using SystemWebUI;
using SystemWebUIWebControls;
using SystemWebUIWebControlsWebParts;
using SystemWebUIHtmlControls;
using SystemXml;namespace PuTianCheng
{
/// <summary>
/// XmlHelper 的摘要说明
/// </summary>
public class XmlHelper
{
public XmlHelper()
{
}/// <summary>
/// 读取数据
/// </summary>
/// <param name="path">路径</param>
/// <param name="node">节点</param>
/// <param name="attribute">属性名,非空时返回该属性值,否则返回串联值</param>
/// <returns>string</returns>
/
使用示列:
XmlHelperRead(path, "/Node", "")
XmlHelperRead(path, "/Node/Element[@Attribute='Name']", "Attribute")
/
public static string Read(string path, string node, string attribute)
{
string value = "";
try
{
XmlDocument doc = new XmlDocument();
docLoad(path);
XmlNode xn = docSelectSingleNode(node);
value = (attributeEquals("") xnInnerText : xnAttributes[attribute]Value);
}
catch { }
return value;
}/// <summary>
/// 插入数据
/// </summary>
/// <param name="path">路径</param>
/// <param name="node">节点</param>
/// <param name="element">元素名,非空时插入新元素,否则在该元素中插入属性</param>
/// <param name="attribute">属性名,非空时插入该元素属性值,否则插入元素值</param>
/// <param name="value">值</param>
/// <returns></returns>
/
使用示列:
XmlHelperInsert(path, "/Node", "Element", "", "Value")
XmlHelperInsert(path, "/Node", "Element", "Attribute", "Value")
XmlHelperInsert(path, "/Node", "", "Attribute", "Value")
/
public static void Insert(string path, string node, string element, string attribute, string value)
{
try
{
XmlDocument doc = new XmlDocument();
docLoad(path);
XmlNode xn = docSelectSingleNode(node);
if (elementEquals(""))
{
if (!attributeEquals(""))
{
XmlElement xe = (XmlElement)xn;
xeSetAttribute(attribute, value);
}
}
else
{
XmlElement xe = docCreateElement(element);
if (attributeEquals(""))
xeInnerText = value;
else
xeSetAttribute(attribute, value);
xnAppendChild(xe);
}
docSave(path);
}
catch { }
}/// <summary>
/// 修改数据
/// </summary>
/// <param name="path">路径</param>
/// <param name="node">节点</param>
/// <param name="attribute">属性名,非空时修改该节点属性值,否则修改节点值</param>
/// <param name="value">值</param>
/// <returns></returns>
/
使用示列:
XmlHelperInsert(path, "/Node", "", "Value")
XmlHelperInsert(path, "/Node", "Attribute", "Value")
/
public static void Update(string path, string node, string attribute, string value)
{
try
{
XmlDocument doc = new XmlDocument();
docLoad(path);
XmlNode xn = docSelectSingleNode(node);
XmlElement xe = (XmlElement)xn;
if (attributeEquals(""))
xeInnerText = value;
else
xeSetAttribute(attribute, value);
docSave(path);
}
catch { }
}/// <summary>
/// 删除数据
/// </summary>
/// <param name="path">路径</param>
/// <param name="node">节点</param>
/// <param name="attribute">属性名,非空时删除该节点属性值,否则删除节点值</param>
/// <param name="value">值</param>
/// <returns></returns>
/
使用示列:
XmlHelperDelete(path, "/Node", "")
XmlHelperDelete(path, "/Node", "Attribute")
/
public static void Delete(string path, string node, string attribute)
{
try
{
XmlDocument doc = new XmlDocument();
docLoad(path);
XmlNode xn = docSelectSingleNode(node);
XmlElement xe = (XmlElement)xn;
if (attributeEquals(""))
xnParentNodeRemoveChild(xn);
else
xeRemoveAttribute(attribute);
docSave(path);
}
catch { }
}
}
}

XmlNode node = xmlDocumentElement;
foreach (XmlNode item in nodeChildNodes)
{
if (item["author"]InnerText == "王明")
item["author"]InnerText = "力强";
}
xmlSave("Xmlfile1xml");

应该可以设置id,根据id获取值
不好意思啦!借用zhangmiger代码一下。
XmlDocument doc = new XmlDocument();
docLoad("//XMLFile1xml");
XmlNodeList nodes = docSelectNodes("/Dimap_Document/IMAGING_DATE/vertex");
foreach (XmlNode node in nodes)
{
if(nodeAttributes["id"]value=="v2"){
ConsoleWriteLine(nodeInnerText);
}
}
ConsoleRead();
XMLFile1xml文件的内容:
<xml version="10" encoding="utf-8" >
<Dimap_Document>
<IMAGING_DATE>
<vertex id="v1">
a
</vertex>
<vertex id="v2">
b
</vertex>
<vertex id="v3">
c
</vertex>
</IMAGING_DATE>
</Dimap_Document>

TiXmlDocumen pXMLDoc = new TiXmlDocument("");
if (pXMLDoc != NULL)
{
pXMLDoc->LoadFile();
}
TiXmlNode pNode = pXMLDoc->FirstChild(""); // 为根节点名称
// 获得元素
TiXmlElement pXmlElement = pNode->ToElement();

// 获得元素的属性
string strAttrib = pXmlElement->Attribute("name") // name为根元素的属性

// 获得元素的text
string strText = pXmlElement->GetText();

// 获得下一个指定名称的元素,一般用于遍历同类元素,多用于while循环中
pXmlElement= pXmlElement->NextSiblingElement("nextElement");

XmlNode xmldocSelect=xmlDocSelectSingleNode("user");

XmlElement el=xmlDocCreateElement("person"); //添加person节点
elSetAttribute("name","风云"); //添加person节点的属性"name"
elSetAttribute("sex","女"); //添加person节点的属性 "sex"
elSetAttribute("age","25"); //添加person节点的属性 "age"

XmlElement xesub1=xmlDocCreateElement("pass"); //添加person节点的里的节点
xesub1InnerText="123";//设置文本节点
elAppendChild(xesub1);
XmlElement xesub2=xmlDocCreateElement("Address");
xesub2InnerText="昆明";//设置文本节点
elAppendChild(xesub2);
xmldocSelectAppendChild(el);
xmlDocSave(ServerMapPath("userxml"));


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/13399696.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-29
下一篇 2023-07-29

发表评论

登录后才能评论

评论列表(0条)

保存