刚初学C#,调用了一个借口,返回XML串,内容是: <DocumentElement>
<fileList>
<fileName>2012102308310.jpg</fileName>
</fileList>
<fileList>
<fileName>2012102309310.jpg</fileName>
</fileList>
<fileList>
<fileName>2012102310310.jpg</fileName>
</fileList>
</DocumentElement>
这样的东西,我想把里面的各个节点上的带数字值(2012102308310.jpg.....)取出来, 该怎么解析呢?求代码
<fileList>
<fileName>2012102308310.jpg</fileName>
</fileList>
<fileList>
<fileName>2012102309310.jpg</fileName>
</fileList>
<fileList>
<fileName>2012102310310.jpg</fileName>
</fileList>
</DocumentElement>
这样的东西,我想把里面的各个节点上的带数字值(2012102308310.jpg.....)取出来, 该怎么解析呢?求代码
string name = string.Empty; //存储你的 .jpg内容
XmlDocument xmldoc = new XmlDocument();
string spath = Server.MapPath("../SA/Services.xml"); //路径
xmldoc.Load(spath);
foreach (XmlNode xNode in xmldoc.DocumentElement.ChildNodes)
{
name += xNode.InnerText; //这个就是你的2012102308310.jpg
}
var nodes = doc.Descendants("fileName");
foreach(XElement item in nodes)
{
MessageBox.Show(item.Value);
}
var query=from i in xml.Elements("fileList").Elements("fileName")
select i;
foreach(var i in query)
{
i.Value就是2012102310310.jpg这样的字符串了
然后你在自己去掉后缀就行了
}
MapServers.Service ss = new MapServers.Service();
string FileName = ss.getFileList("title");
FileName =" <DocumentElement>
<fileList>
<fileName>2012102308310.jpg</fileName>
</fileList>
<fileList>
<fileName>2012102309310.jpg</fileName>
</fileList>
<fileList>
<fileName>2012102310310.jpg</fileName>
</fileList>
</DocumentElement>
";
不是xx.xml 这样的XML文件呀, 不能读啊
doc.Load("xmlpath");//xml文件路径
string filename= doc.DocumentElement.SelectSingleNode("fileName").innertext.tostring();
filename就是你要取的拿个2012102308310.jpg的值
doc.LoadXml("xmlContent");//从指定的xml字符串加载
for(int i=1;i<str.Length;i++)
{
MessageBox.Show(str.Split('<')[0]);
}
string[] str =FileName.Split(new string[]{"<fileName>"}, StringSplitOptions.RemoveEmptyEntries);
for(int i=1;i<str.Length;i++)
{
MessageBox.Show(str[i].Split('<')[0]);
}
var tradeNodes=from node in doc.Elements("fileList") select node;
foreach(var node in tradeNodes)
{
node.Element("fileName").Value
}
<fileList>
<fileName>2012102308310.jpg</fileName>
</fileList>
<fileList>
<fileName>2012102309310.jpg</fileName>
</fileList>
<fileList>
<fileName>2012102310310.jpg</fileName>
</fileList>
</DocumentElement>";
System.Xml.XmlDocument xmlDoc= new System.Xml.XmlDocument();
xmlDoc.LoadXml(str);
string fileName= xmlDoc.DocumentElement.InnerText;
<?xml version=\"1.0\" encoding=\"gb2312\"?><DocumentElement>
<fileList>
<fileName>2012102308310.jpg</fileName>
</fileList>
<fileList>
<fileName>2012102309310.jpg</fileName>
</fileList>
<fileList>
<fileName>2012102310310.jpg</fileName>
</fileList>
</DocumentElement>
";
是这样的内容的话, 通过 doc.Load(FileName) 这种方法怎么得到很好的解决呢
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Application.StartupPath + "/file/aa.xml");
XmlNodeList lstNode = xmlDoc.DocumentElement.ChildNodes;
foreach (XmlNode node in lstNode)
{
XmlNodeList lstChildNode = node.ChildNodes;
foreach (XmlNode childNode in lstChildNode)
{
result = childNode.InnerText;
}
}
return result;