本人对.net读取xml不甚了解.....请大家为小弟指点迷津xml文件 <?xml version="1.0" encoding="UTF-8" ?>
- <root>
- <tRecords>
- <tRecord guid="{79980701-ba95-417f-b5d9-8909878ee181}">
<RReason>BB</RReason>
<RReasonNo />
<RLocation />
<RTime />
<REName />
<REWorkUnit />
<REPosition />
<REAddress />
<REZip />
<REPhone />
<RESex>1</RESex>
<REAge />
<RWName />
<RWZip />
<RWAddress />
<RWPhone />
<RAPerson />
<RRPerson />
<RLocked>0</RLocked>
<RCreator>admin</RCreator>
<RDate>2010-10-25 14:38:31</RDate>
<RELicence />
</tRecord>
- <QAProof guid="{79980701-ba95-417f-b5d9-8909878ee181}">
<ProofID>2|4|5</ProofID>
<Proof>示意图;船舶证书;船员适任证书</Proof>
<QACategory>55</QACategory>
<QA> 我们是海事调查官</QA>
</QAProof>
</tRecords>
</root>
代码
Stream myStream = collection[i].InputStream;//输入流
XmlDocument doc=new XmlDocument();
doc.Load(myStream); XmlNodeList xnl=doc.DocumentElement.SelectNodes("tRecords"); foreach(XmlNode xmlNode in xnl)
{
XmlNode Cxn=xmlNode.SelectSingleNode("tRecord");
string s=Cxn.Attributes["RReason"].Value;
XmlNode xn=xmlNode.SelectSingleNode("QAProof");
string str=xn.Attributes["ProofID"].Value;
}为什么我这样读取,缺一直报
Cxn.Attributes["RReason"].Value 错误: 对象“Cxn.Attributes”没有索引器 请问我该如何读取,因为在tRecords下面有tRecord和QAProof两个节点
- <root>
- <tRecords>
- <tRecord guid="{79980701-ba95-417f-b5d9-8909878ee181}">
<RReason>BB</RReason>
<RReasonNo />
<RLocation />
<RTime />
<REName />
<REWorkUnit />
<REPosition />
<REAddress />
<REZip />
<REPhone />
<RESex>1</RESex>
<REAge />
<RWName />
<RWZip />
<RWAddress />
<RWPhone />
<RAPerson />
<RRPerson />
<RLocked>0</RLocked>
<RCreator>admin</RCreator>
<RDate>2010-10-25 14:38:31</RDate>
<RELicence />
</tRecord>
- <QAProof guid="{79980701-ba95-417f-b5d9-8909878ee181}">
<ProofID>2|4|5</ProofID>
<Proof>示意图;船舶证书;船员适任证书</Proof>
<QACategory>55</QACategory>
<QA> 我们是海事调查官</QA>
</QAProof>
</tRecords>
</root>
代码
Stream myStream = collection[i].InputStream;//输入流
XmlDocument doc=new XmlDocument();
doc.Load(myStream); XmlNodeList xnl=doc.DocumentElement.SelectNodes("tRecords"); foreach(XmlNode xmlNode in xnl)
{
XmlNode Cxn=xmlNode.SelectSingleNode("tRecord");
string s=Cxn.Attributes["RReason"].Value;
XmlNode xn=xmlNode.SelectSingleNode("QAProof");
string str=xn.Attributes["ProofID"].Value;
}为什么我这样读取,缺一直报
Cxn.Attributes["RReason"].Value 错误: 对象“Cxn.Attributes”没有索引器 请问我该如何读取,因为在tRecords下面有tRecord和QAProof两个节点
没人来。现在能读出来节点了,但还是读不到属性值,就是那个guid的
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Server.MapPath("~/api/website.xml"));//读取XML文件
XmlNodeList xmlNode = xmldoc.SelectNodes("web/webins");//查找元素
XmlNode xmlno = xmlNode.Item(0);
this.webname.Value = xmlno["title"].InnerText;//网站标题
this.adderss.Value = xmlno["address"].InnerText;//网站地址
this.logoimg.Src = xmlno["logourl"].InnerText;//logo地址
if (!IsPostBack)
{
StringBuilder sb = new StringBuilder();
XmlDocument xml = new XmlDocument();
xml.Load(Server.MapPath(@"/XMLFile1.xml"));
XmlNodeList nodelist = xml.SelectSingleNode("root").SelectSingleNode("tRecords").ChildNodes;
foreach (XmlNode node in nodelist)
{
XmlElement element = (XmlElement)node;
sb.Append(element.Name + " " + element.GetAttribute("guid") + "<br/>");
}
Response.Write(sb.ToString());
}
xmlDoc.Load("xml.xml");
XmlNodeList xnl = xmlDoc.GetElementsByTagName("tRecords");
foreach (XmlNode xn in xnl)
{
XmlElement xe = xn as XmlElement;
string strAttribute = xe.GetAttribute("guid");
}
差不多就这个方法。。