使用XPath解析XML为CSV。创建外环或者这是正确的方式?

问题描述 投票:0回答:1

我检索HTTP \ XML响应和解析到了把一个CSV文件。我能做到这一点,如果只有一个反应是有,但更多的时候比1个的每个节点是分析所有同名的第一个节点,那么所有同名的第二个节点等,而不是节点1,节点2,节点1,节点2的我有很丑陋,我相信可以写更好。我简单rquest是怎样把一个循环解决此所以它每次只返回一个数据集或XPath的在获得价值对帐单上每个循环shuld我将结合所有德forEachs?或者我应该使用不同的充技术,如XMLReader的还是其他什么东西? XML文件

<?xml version="1.0" encoding="UTF-8" ?>
<multistatus xmlns="DAV:">
 <response>
  <href>http://192.168.0.2:80/User-84</href>
  <propstat>
    <prop>
        <handle>
            <dsref handle="User-84"><displayname>George Thurogood</displayname>
                <firstname>George</firstname>
                <lastname>Thurogood</lastname>
                <username>cz001234</username>
             </dsref>
         </handle>
         <email>[email protected]</email>
         <isActive>1</isActive>
         <domain>Lab.local</domain>
     </prop>
     <status>HTTP/1.1 200 OK</status>
 </propstat>
 <propstat>
    <prop>
        <firstname/>
        <lastname/>
     </prop>
     <status>HTTP/1.1 404 Not Found</status>
 </propstat>
</response>
<response>
<href>http://192.168.0.2:80/User-83</href>
<propstat>
    <prop>
        <handle>
            <dsref handle="User-83">
                <displayname>Molly Meldrum</displayname>
                <firstname>Molly</firstname>
                <lastname>Meldrum</lastname>
                <username>cz026381</username>
            </dsref>
        </handle>
        <email>[email protected]</email>
        <isActive>1</isActive>
        <domain>Lab.local</domain>
    </prop>
    <status>HTTP/1.1 200 OK</status>
</propstat>
<propstat>
    <prop>
        <firstname/>
        <lastname/>
    </prop>
    <status>HTTP/1.1 404 Not Found</status>
</propstat>

我目前使用的代码

if (response.StatusCode == HttpStatusCode.OK)
            {

            }
            response.Close();
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(responseString);P
            responseString = "";

            //create the namespace manager
            var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
            nsmgr.AddNamespace("DAV", "DAV:");
            XmlNodeList elemList = xmlDoc.SelectNodes("//DAV:prop", nsmgr);



              foreach (XmlNode node in xmlDoc.SelectNodes("//DAV:prop/DAV:handle/DAV:dsref", nsmgr))
              {
                strNode = node.Attributes["handle"].Value;
                strFNnode = 
                //strNode = strNode + " - " + node.InnerText;
                //responseString += strNode + " , " + "\r\n";            
              }

              foreach (XmlNode node in xmlDoc.SelectNodes("//DAV:prop/DAV:handle/DAV:dsref/DAV:displayname", nsmgr))
              {
                //strNode = node.Attributes["handle"].Value;
                strNode = strNode + "," + node.InnerText;
                //responseString += strNode + " ,  " + "\r\n";                    
              }
              foreach (XmlNode node in xmlDoc.SelectNodes("//DAV:prop/DAV:handle/DAV:dsref/DAV:firstname", nsmgr))
              {
                //strNode = node.Attributes["handle"].Value;
                   strNode = strNode + "," + node.InnerText;
                // responseString += strNode + " ,  " + "\r\n";
              }
              foreach (XmlNode node in xmlDoc.SelectNodes("//DAV:prop/DAV:handle/DAV:dsref/DAV:lastname", nsmgr))
              {
                //strNode = node.Attributes["handle"].Value;
                strNode = strNode + "," + node.InnerText;
                // responseString += strNode + " ,  " + "\r\n";
              }
              foreach (XmlNode node in xmlDoc.SelectNodes("//DAV:prop/DAV:handle/DAV:dsref/DAV:username", nsmgr))
              {
                //strNode = node.Attributes["handle"].Value;
                  strNode = strNode + "," + node.InnerText;
                // responseString += strNode + " ,  " + "\r\n";
              }
              foreach (XmlNode node in xmlDoc.SelectNodes("//DAV:prop/DAV:email", nsmgr))
              {
                //strNode = node.Attributes["handle"].Value;
                strNode = strNode + "," + node.InnerText;
                // responseString += strNode + " ,  " + "\r\n";
              }
              foreach (XmlNode node in xmlDoc.SelectNodes("//DAV:prop/DAV:isActive", nsmgr))
              {
                //strNode = node.Attributes["handle"].Value;
                strNode = strNode + "," + node.InnerText;
                // responseString += strNode + "," + "\r\n";
              }
              foreach (XmlNode node in xmlDoc.SelectNodes("//DAV:prop/DAV:domain", nsmgr))
              {
                //strNode = node.Attributes["handle"].Value;
                strNode = strNode + "," + node.InnerText;
                responseString += strNode + "\r\n";
              }

            return temp;

这应返回

User-84,George Thurogood,George,Thurogood,cz001234,[email protected],1,lab.local
User-83,Molly Meldrun,Molly,Meldrum,cz026381,[email protected],1,lab.local
c# xpath xml-parsing
1个回答
2
投票

尝试XML LINQ:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;


namespace ConsoleApplication100
{
    class Program
    {
        const string XML_FILENAME = @"c:\temp\test.xml";
        const string CSV_FILENAME = @"c:\temp\test.csv";
        static void Main(string[] args)
        {
            PropStat.ParseXML(XML_FILENAME);
            PropStat.CreateCSV(CSV_FILENAME);
        }


    }
    public class PropStat
    {
        public static List<PropStat> propStats = new List<PropStat>();

        public string href { get; set; }
        public string handle { get; set; }
        public string firstname { get; set; }
        public string lastname { get; set; }
        public string username { get; set; }
        public string email { get; set; }
        public Boolean?  isactive { get; set; }
        public string domain { get; set; }
        public string status { get; set; }

        public static void ParseXML(string filename)
        {
            XDocument doc = XDocument.Load(filename);
            XElement root = doc.Root;
            XNamespace ns = root.GetDefaultNamespace();

            foreach (XElement response in doc.Descendants(ns + "response"))
            {
                string href = (string)response.Element(ns + "href");

                foreach(XElement propstat in response.Descendants(ns + "propstat"))
                {
                    PropStat newPropStat = new PropStat();
                    propStats.Add(newPropStat);
                    newPropStat.href = href;

                    XElement dsref = propstat.Descendants(ns + "dsref").FirstOrDefault();

                    if (dsref != null)
                    {
                        newPropStat.handle = (string)dsref.Attribute("handle");
                    }

                    newPropStat.firstname = (string)propstat.Descendants(ns + "firstname").FirstOrDefault();
                    newPropStat.lastname = (string)propstat.Descendants(ns + "lastname").FirstOrDefault();
                    newPropStat.username = (string)propstat.Descendants(ns + "username").FirstOrDefault();
                    newPropStat.email = (string)propstat.Descendants(ns + "email").FirstOrDefault();
                    newPropStat.isactive = (string)propstat.Descendants(ns + "isactive").FirstOrDefault() == "0" ? false : true;
                    newPropStat.domain = (string)propstat.Descendants(ns + "domain").FirstOrDefault();
                    newPropStat.status = (string)propstat.Descendants(ns + "status").FirstOrDefault();
                }

            }
        }
        public static void CreateCSV(string filename)
        {
            StreamWriter writer = new StreamWriter(filename);
            string header = string.Join(",",
                "href",
                "handle",
                "firstname",
                "lastname",
                "username",
                "email",
                "isactive",
                "domain",
                "status");

            writer.WriteLine(header);


            foreach (PropStat propstat in propStats)
            {
                string data = string.Join(",",
                    propstat.href,
                    propstat.handle,
                    propstat.firstname,
                    propstat.lastname,
                    propstat.username,
                    propstat.email,
                    propstat.isactive,
                    propstat.domain,
                    propstat.status);

                writer.WriteLine(data);
            }
            writer.Flush();
            writer.Close();
        }
    }



}
© www.soinside.com 2019 - 2024. All rights reserved.