解析XML数据

前端之家收集整理的这篇文章主要介绍了解析XML数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

WebForm1.aspx代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
        <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>

WebForm1.aspx.cs代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        string EcrmMessage = string.Empty;

        protected void Page_Load(object sender,EventArgs e)
        {
            LoadXML();
            Label1.Text = EcrmMessage;

            WebServiceXML();
            Label2.Text = EcrmMessage;
        }

        /// <summary>
        /// 访问本地xml文件返回xml数据
        /// </summary>
        public void LoadXML()
        {
            string result = string.Empty;
            string storeNumber = string.Empty;
            string currentStoreNumber = "0205";

            string xmlFile = Server.MapPath("XMLFile1.xml");
            XmlDocument doc = new XmlDocument();
            doc.Load(xmlFile);

            StringBuilder str = new StringBuilder();
            XmlNodeList nodes = doc.GetElementsByTagName("Store");
            foreach (XmlNode item in nodes)
            {
                storeNumber = ReadNode(item,"StoreNumber");
                if (storeNumber.Length == 1)
                {
                    storeNumber = "000" + storeNumber;
                }
                else if (storeNumber.Length == 2)
                {
                    storeNumber = "00" + storeNumber;
                }
                else if (storeNumber.Length == 3)
                {
                    storeNumber = "0" + storeNumber;
                }
                if (storeNumber == currentStoreNumber)
                {
                    result = ReadNode(item,"nofollowUp");
                    EcrmMessage = result;
                    break;
                }
            }
        }

        /// <summary>
        /// 访问web service返回xml数据
        /// </summary>
        public void WebServiceXML()
        {
            string result = string.Empty;
            string storeNumber = string.Empty;
            string currentStoreNumber = "0206";
            
            ///调用web service
            ecrm.WebsiteIntegrationSoapClient client = new ecrm.WebsiteIntegrationSoapClient();

            XmlNode list = client.GetFollowUpCount("***","***",DateTime.Now.AddDays(-30),DateTime.Now);
            XmlNodeList subNodes = ((XmlElement)list).GetElementsByTagName("Store");
            EcrmMessage = "0";

            foreach (XmlNode item in subNodes)
            {
                storeNumber = ReadNode(item,"nofollowUp");
                    EcrmMessage = result;
                    break;
                }
            }
        }

        private string ReadNode(XmlNode node,string tagName)
        {
            string result = "";
            XmlNodeList subNodes = ((XmlElement)node).GetElementsByTagName(tagName);
            if (subNodes.Count == 1)
            {
                result = subNodes[0].InnerText;
            }
            return result;
        }
    }
}

XMLFile1.xml文件

<?xml version="1.0" encoding="utf-8" ?>
<Stores>
  <Store>
    <CSSiteID>700889</CSSiteID>
    <StoreNumber>205</StoreNumber>
    <OpenNoContact>8</OpenNoContact>
    <nofollowUp>2</nofollowUp>
  </Store>
  <Store>
    <CSSiteID>700890</CSSiteID>
    <StoreNumber>206</StoreNumber>
    <OpenNoContact>11</OpenNoContact>
    <nofollowUp>6</nofollowUp>
  </Store>
  <Store>
    <CSSiteID>700891</CSSiteID>
    <StoreNumber>207</StoreNumber>
    <OpenNoContact>5</OpenNoContact>
    <nofollowUp>3</nofollowUp>
  </Store>
</Stores>
原文链接:https://www.f2er.com/xml/297166.html

猜你在找的XML相关文章