代码之家  ›  专栏  ›  技术社区  ›  Martina

用c[复制]读取.ftp文件

  •  0
  • Martina  · 技术社区  · 7 年前

    如何读取和解析c中的xml文件?

    0 回复  |  直到 7 年前
        1
  •  412
  •   Wolf5    10 年前

    从字符串或文件读取XML的XML文档。

    XmlDocument doc = new XmlDocument();
    doc.Load("c:\\temp.xml");
    

    或

    doc.LoadXml("<xml>something</xml>");
    

    然后在它下面找到一个节点

    XmlNode node = doc.DocumentElement.SelectSingleNode("/book/title");
    

    或

    foreach(XmlNode node in doc.DocumentElement.ChildNodes){
       string text = node.InnerText; //or loop through its children as well
    }
    

    然后像这样读取节点内的文本

    string text = node.InnerText;
    

    或读取属性

    string attr = node.Attributes["theattributename"]?.InnerText
    

    始终检查属性[“something”]上的空值,因为如果该属性不存在,它将为空。

        2
  •  188
  •   LW001 Rohan Muhammad    7 年前

    LINQ to XML 例子:

    // Loading from a file, you can also load from a stream
    var xml = XDocument.Load(@"C:\contacts.xml");
    
    
    // Query the data and write out a subset of contacts
    var query = from c in xml.Root.Descendants("contact")
                where (int)c.Attribute("id") < 4
                select c.Element("firstName").Value + " " +
                       c.Element("lastName").Value;
    
    
    foreach (string name in query)
    {
        Console.WriteLine("Contact's Full Name: {0}", name);
    }
    

    参考文献 : 功能结构 在MSDN

        3
  •  15
  •   Grant Winney    13 年前

    下面是我为阅读XML站点地图而编写的应用程序:

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms; 
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;
    using System.Data;
    using System.Xml;
    
    namespace SiteMapReader
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Please Enter the Location of the file");
    
                // get the location we want to get the sitemaps from 
                string dirLoc = Console.ReadLine();
    
                // get all the sitemaps 
                string[] sitemaps = Directory.GetFiles(dirLoc);
                StreamWriter sw = new StreamWriter(Application.StartupPath + @"\locs.txt", true);
    
                // loop through each file 
                foreach (string sitemap in sitemaps)
                {
                    try
                    {
                        // new xdoc instance 
                        XmlDocument xDoc = new XmlDocument();
    
                        //load up the xml from the location 
                        xDoc.Load(sitemap);
    
                        // cycle through each child noed 
                        foreach (XmlNode node in xDoc.DocumentElement.ChildNodes)
                        {
                            // first node is the url ... have to go to nexted loc node 
                            foreach (XmlNode locNode in node)
                            {
                                // thereare a couple child nodes here so only take data from node named loc 
                                if (locNode.Name == "loc")
                                {
                                    // get the content of the loc node 
                                    string loc = locNode.InnerText;
    
                                    // write it to the console so you can see its working 
                                    Console.WriteLine(loc + Environment.NewLine);
    
                                    // write it to the file 
                                    sw.Write(loc + Environment.NewLine);
                                }
                            }
                        }
                    }
                    catch { }
                }
                Console.WriteLine("All Done :-)"); 
                Console.ReadLine(); 
            }
    
            static void readSitemap()
            {
            }
        }
    }
    

    粘贴箱上的代码 http://pastebin.com/yK7cSNeY

        4
  •  11
  •   Dirk Vollmar    17 年前

    有很多方法,有些:

    • XmlSerializer。使用目标架构的类 您要读取-使用XmlSerializer 将XML中的数据加载到 类的实例。
    • LINQ 2 XML
    • XmlTextReader。
    • XML文档
    • xpathdocument(只读访问)
        5
  •  7
  •   Grzenio    17 年前

    您可以:

    示例见所提供的MSDN页面

        6
  •  7
  •   Will    17 年前

    Linq to XML.

    另外,vb.net通过编译器比c有更好的xml解析支持。如果你有选择和欲望, check it out.

        7
  •  5
  •   prasanna venkatesh    9 年前

    可以使用数据集读取XML字符串。

    var xmlString = File.ReadAllText(FILE_PATH);
    var stringReader = new StringReader(xmlString);
    var dsSet = new DataSet();
    dsSet.ReadXml(stringReader);
    

    为了信息发布这个。

        8
  •  2
  •   Quintin Balsdon    9 年前

    退房 XmlTextReader 例如类。

        9
  •  0
  •   Vishal Kotak    11 年前
      public void ReadXmlFile()
        {
            string path = HttpContext.Current.Server.MapPath("~/App_Data"); // Finds the location of App_Data on server.
            XmlTextReader reader = new XmlTextReader(System.IO.Path.Combine(path, "XMLFile7.xml")); //Combines the location of App_Data and the file name
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        break;
                    case XmlNodeType.Text:
                        columnNames.Add(reader.Value);
                        break;
                    case XmlNodeType.EndElement:
                        break;
                }
            }
        }
    

    您可以避免第一个语句,只需在xmltextreader的构造函数中指定路径名。

        10
  •  0
  •   nkalfov    9 年前

    有不同的方法,取决于你想去哪里。 xmldocument比xdocument轻,但是如果您希望最少地验证字符串是否包含xml,那么正则表达式可能是您可以做出的最快、最轻的选择。例如,我用specflow为我的api实现了Smoke测试,我想测试其中一个结果是否是有效的XML,然后我将使用正则表达式。但是如果我需要从这个xml中提取值,那么我将使用xdocument来解析它,以更快、更少的代码完成它。或者,如果我必须使用一个大的XML(有时我使用大约100万行甚至更多的XML),我会使用XML文档;然后我甚至可以逐行读取它。为什么?尝试在visual studio中以私有字节打开超过800mb的文件;即使在生产环境中,对象也不应大于2gb。你可以用粗花呢,但你不应该。如果您需要解析一个包含很多行的文档,那么这个文档可能是csv。

    我写了这篇评论,因为我看到了许多带有xdocument的示例。xdocument不适合大型文档,或者只想验证内容是否是xml有效的。如果您想检查XML本身是否有意义,那么您需要模式。

    我也否决了建议的答案,因为我相信它需要上面的信息。假设我需要验证2亿个XML(每小时10次)是否是有效的XML。xdocument将浪费大量资源。

    prasanna venkatesh还声明,您可以尝试将字符串填充到数据集,它也将指示有效的xml。