您的位置:首页 > 其它

xml相关操作

2009-04-26 15:54 204 查看
xml相关操作

xmlDocument 增删改查

增加
XmlElement xesub3=xmlDoc.CreateElement("price");
xesub3.InnerText="58.3";
xe1.AppendChild(xesub3);

修改
XmlElement xe=(XmlElement)xn;//将子节点类型转换为XmlElement类型
xe.SetAttribute("genre","update李赞红");//则修改该属性为“update李赞红”
xmlDoc.Save("bookstore.xml");



XmlNode xn=xmlDoc.SelectSingleNode("bookstore");


xe.RemoveAll();//删除该节点的全部内容

---对此类xml处理

<?xml version="1.0" encoding="utf-8"?>
<PCONFIG>
<Receivers>
<Receiver ID="1" NodeID="3" NodeType="30" UserID="10002" Name="中国" ActorType="Worker_" ReceiveType="" />
</Receivers>
</PCONFIG>

public String getStrXML(String xmlvalue,String root,String aname)
{
String xv="";
XmlDocument xd = new XmlDocument();
xd.LoadXml(xmlvalue);
XmlNode xn = xd.SelectSingleNode(root);
xv = xn.Attributes[aname].Value ;
return xv;
}

整理后的方法类

#region Xml读取
/// <summary>
/// Xml读取
/// </summary>
/// <param name="fid"></param>
/// <returns></returns>

public String getXmlTextReader(String fid)
{

String path = Xmlpath;
String rtxt = "";
if (path != null)
{
XmlTextReader xtr = new XmlTextReader(path);
while (xtr.Read())
{
if (xtr.LocalName.ToLower().Equals(fid.Trim().ToLower()))
{
rtxt = xtr.ReadElementString();
break;
}
}
xtr.Close();
}
else
{
throw new Exception("getXmlTextReader:xml文件路径不正确,请重新配置");
}
return rtxt;
}

/// <summary>
/// 将所xml的全部内容转成String
/// </summary>
/// <returns></returns>
public String getXmlDocument(String root)
{
XmlDocument xd = new XmlDocument();
if (Xmlpath != null)
{
xd.Load(Xmlpath);
}
else
{
throw new Exception("getXmlDocument:xml文件路径不正确,请重新配置");
}
String stxt = root + "/" ;
XmlNode xn = xd.SelectSingleNode(stxt);
return xn.InnerText;
}
/// <summary>
/// 获取xml字符串的指定目录值
/// </summary>
/// <param name="xmlvalue"></param>
/// <param name="xname"></param>
/// <returns></returns>
public String getStrXML(String xmlvalue,String root,String aname)
{
String xv="";
XmlDocument xd = new XmlDocument();
xd.LoadXml(xmlvalue);
XmlNode xn = xd.SelectSingleNode(root);
xv = xn.Attributes[aname].Value;
return xv;
}

/// <summary>
/// 根据root和name来定位数值
/// </summary>
/// <returns></returns>
public String getXmlDocument(String root,String name)
{
String txt = "";
XmlDocument xd = new XmlDocument();
if (Xmlpath != null)
{
xd.Load(Xmlpath);
}
else
{
throw new Exception("getXmlDocument:xml文件路径不正确,请重新配置");
}
String stxt = root + "/" + name;
XmlNode xn = xd.SelectSingleNode(stxt);
return xn.InnerText;
}
#endregion
#region Xml写入
/// <summary>
/// Xml写入
/// </summary>
/// <param name="fid"></param>
/// <returns></returns>
public void setXmlDocument(String root,String name, String value)
{
XmlDocument xd = new XmlDocument();
if (Xmlpath != null)
{
xd.Load(Xmlpath);
}
else
{
throw new Exception("setXmlDocument:xml文件路径不正确,请重新配置");
}
XmlNode xn = xd.SelectSingleNode(root+"/"+ name);
xn.InnerText = value;
xd.Save(Xmlpath);
}

public void setXmlDocument(String xmlvalue)
{
XmlDocument xd = new XmlDocument();

if (Xmlpath != null)
{
xd.LoadXml(xmlvalue);
}
else
{
throw new Exception("setXmlDocument:xml文件路径不正确,请重新配置");
}
xd.Save(Xmlpath);
}

#endregion
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: