xml中如何判断一个节点node1中有没有属性arr1? XmlElement test=new XmlElement(); test.Attributes.Count;//接点数目 if(test.Attributes["arr1"].Value==null) { //没有属性 } else //有属性 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 using System.Xml;XmlDocument xmldoc = new XmlDocument();xmldoc.Load("YourXMLFilePath.xml");XmlNode node = xmldoc.SelectSingleNode("//node1[@arr1]");if (node != null){//exists} XmlNode test= new XmlNode(); test.Attributes.Count; //接点数目 if(test.Attributes["arr1"].Value==null) { //没有属性 } else //有属性 也可以在xpath里面指定XmlDocument xmlDom=new XmlDocument();xmlDom.Load("myxmlfile.xml");XmlNodeList nc=xmlDom.SelectNodes("YourPath/MyNode[@Arr1]");//找出YourPath下面具有Attr1属性的MyNode节点 谢谢两位!再问一下,如果节点node1中没有arr1属性,我用下面语句: _strTemp = node1.Attributes["arr1"].Value;_strTemp是得到了null值呢,还是抛出一个异常,出错。_strTemp是string类型的,如果是int类型的又会怎么样呢。 应该会有空引用的异常,属性可以动态加,如下:XmlDocument doc = new XmlDocument(); doc.LoadXml("<book xmlns:bk='urn:samples' bk:ISBN='1-861001-5-5'>" + "<title>Pride And PreJudice</title>"+ "</book>"); XmlElement root = doc.DocumentElement; //add a new attributesroot.SetAttribute("genre", "urn:samples", "novel"); XmlAttribute attr = doc.CreateAttribute("publisher");attr.Value = "WorldWide Publishing"; root.SetAttributeNode(attr); string strTemp = "";if (node1.Attributes["arr1"] != null){ strTemp = node1.Attributes["arr1"].Value;} 满分求一正则,同时替换多个项 问一个简单问题 asp.net 怎样实现在asp页面的textbox中按回车执行按钮事件 理解能力较差 有地方不懂以已标明 希望高手解释或推荐读物jscript .net中的资源文件有什么用啊 Checkboxlist 如何调格式 有关DATALIST问题 结交好友,重点讨论邮件收发和短信收发。。 555~~~穷死了,最后40分了,希望大家帮我看看,谢谢! 为什么ASP。NET 没有项目的概念,它的项目文件是什么,有何区别? NET 创建类有哪几种方式啊? 初学者的问题:我怎么提交页面
xmldoc.Load("YourXMLFilePath.xml");
XmlNode node = xmldoc.SelectSingleNode("//node1[@arr1]");
if (node != null)
{
//exists
}
test.Attributes.Count; //接点数目
if(test.Attributes["arr1"].Value==null)
{
//没有属性
}
else //有属性
XmlDocument xmlDom=new XmlDocument();
xmlDom.Load("myxmlfile.xml");
XmlNodeList nc=xmlDom.SelectNodes("YourPath/MyNode[@Arr1]");//找出YourPath下面具有Attr1属性的MyNode节点
再问一下,如果节点node1中没有arr1属性,我用下面语句:
_strTemp = node1.Attributes["arr1"].Value;
_strTemp是得到了null值呢,还是抛出一个异常,出错。_strTemp是string类型的,如果是int类型的又会怎么样呢。
doc.LoadXml("<book xmlns:bk='urn:samples' bk:ISBN='1-861001-5-5'>" +
"<title>Pride And PreJudice</title>"+
"</book>");
XmlElement root = doc.DocumentElement;
//add a new attributes
root.SetAttribute("genre", "urn:samples", "novel");
XmlAttribute attr = doc.CreateAttribute("publisher");
attr.Value = "WorldWide Publishing";
root.SetAttributeNode(attr);
if (node1.Attributes["arr1"] != null)
{
strTemp = node1.Attributes["arr1"].Value;
}