刚初学C#,调用了一个借口,返回XML串,内容是: <DocumentElement>
                  <fileList>
                     <fileName>2012102308310.jpg</fileName>
                 </fileList>
                 <fileList>
                     <fileName>2012102309310.jpg</fileName>
                 </fileList>
                 <fileList>
                     <fileName>2012102310310.jpg</fileName>
                 </fileList>
               </DocumentElement>
这样的东西,我想把里面的各个节点上的带数字值(2012102308310.jpg.....)取出来, 该怎么解析呢?求代码

解决方案 »

  1.   


                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
                }
      

  2.   

    Linq            XDocument doc = XDocument.Load("c:\\temp\\1.txt");
                var nodes = doc.Descendants("fileName");
                foreach(XElement item in nodes)
                {
                    MessageBox.Show(item.Value);
                }
      

  3.   

    XElement xml=XElement.Parse(xmlstring);
    var query=from i in xml.Elements("fileList").Elements("fileName")
              select i;
    foreach(var i in query)
    {
        i.Value就是2012102310310.jpg这样的字符串了
    然后你在自己去掉后缀就行了
    }
      

  4.   

    我获取的是字符串,
     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文件呀, 不能读啊
      

  5.   

    System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                doc.Load("xmlpath");//xml文件路径
               string  filename= doc.DocumentElement.SelectSingleNode("fileName").innertext.tostring();
    filename就是你要取的拿个2012102308310.jpg的值
      

  6.   

      doc.Load("xmlpath");  这个方法是加载文件的   我的是字符串
      

  7.   


    doc.LoadXml("xmlContent");//从指定的xml字符串加载
      

  8.   

    string[] str =FileName.Split(new string[]{"<fileName>"}, StringSplitOptions.RemoveEmptyEntries);
    for(int i=1;i<str.Length;i++)
    {
    MessageBox.Show(str.Split('<')[0]);
    }
      

  9.   

     doc.Load(FileName);  有异常 “路径中具有非法字符。” 
      

  10.   

    呃,上面写漏了点,是这样的:
    string[] str =FileName.Split(new string[]{"<fileName>"}, StringSplitOptions.RemoveEmptyEntries);
    for(int i=1;i<str.Length;i++)
    {
    MessageBox.Show(str[i].Split('<')[0]);
    }
      

  11.   

    XElement doc=  XElement.Parse(str);
    var tradeNodes=from node in doc.Elements("fileList") select node;
    foreach(var node in tradeNodes)
    {
       node.Element("fileName").Value
    }
      

  12.   

      string str =@" <DocumentElement>
      <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;
      

  13.   

    http://topic.csdn.net/u/20110208/21/1fcd05cc-fbe4-4487-89d6-4f85f02248e3.html
      

  14.   

    happySnow_zhe  的方法能解决这个我问题,如果: string FileName ="
    <?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) 这种方法怎么得到很好的解决呢
      

  15.   

    string result = "";
    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;