在C#中从XML读取多个值

时间:2023-02-10 23:47:56

i am having a xml file

我有一个xml文件

<Actual-External-Terminals>
    <Actual-External-Terminal>
      <Party-Id value="EXTRA:77440" />
      <Name value="77440" />
      <Dial-String value="77440" />
      <IP-ISDN-SIP value="IP" />
      <Total-Connection-Time value="0s" />
      <Failing-Attempts value="0" />
      <Last-Failure-Cause value="N/A" />
      <List-of-Connection-Records>
        <Connection Call-Rate="768" Call-Type="Video" ConnectionTime="" Dialin-Dialout="Dial-in" Disconnection-Time="2012-02-16T13:33:32Z" Over-GW-port-limit="false" Over-MCU-port-limit="false" Reason-Disconnection="Normal" />
      </List-of-Connection-Records>
    </Actual-External-Terminal>

In the above xml i want to get all the value of connection section please help me out thanks can't use Linq to xml because its .net framework 2.0 and desktop App

在上面的xml我想获得连接部分的所有价值请帮助我谢谢你不能使用Linq到xml,因为它的.net framework 2.0和桌面应用程序

regards wasif

关于wasif

2 个解决方案

#1


1  

System.Xml is a way. As follows:

System.Xml是一种方式。如下:

  using System.Xml;
// Name space & class declarations...
static void ReadXml(string filePath)
        {
            //These would be kept in a settings file but constants for this example
            const string CONNECTION_LISTING_NODE_NAME = "List-of-Connection-Records";
            const string CONNECTION_NODE_NAME = "Connection";
            const string CALL_RATE_ATTRIBUTE_NAME = "Call-Rate";

            //Load xml
            var doc = new XmlDocument();
            doc.Load(filePath);
            var root = doc.FirstChild;
            var connectionRecordLists = doc.SelectNodes(String.Format("//{0}",CONNECTION_LISTING_NODE_NAME));
            if (connectionRecordLists == null) return;
            for (var i = 0; i < connectionRecordLists.Count; i++)
            {
                var connections = connectionRecordLists[i].SelectNodes(CONNECTION_NODE_NAME);
                if (connections == null) continue;
                for (var j = 0; j < connections.Count; j++)
                {
                    if (connections[j].Attributes != null
                        && connections[j].Attributes[CALL_RATE_ATTRIBUTE_NAME] != null)
                    {
                        Console.WriteLine(connections[j].Attributes[CALL_RATE_ATTRIBUTE_NAME].Value);
                    }

                }
            }
        }

#2


0  

if your xml file has a fixed element and attributes, what about XmlSerializer Class? http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer(v=vs.80).aspx

如果您的xml文件具有固定的元素和属性,那么XmlSerializer类呢? http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer(v=vs.80).aspx

#1


1  

System.Xml is a way. As follows:

System.Xml是一种方式。如下:

  using System.Xml;
// Name space & class declarations...
static void ReadXml(string filePath)
        {
            //These would be kept in a settings file but constants for this example
            const string CONNECTION_LISTING_NODE_NAME = "List-of-Connection-Records";
            const string CONNECTION_NODE_NAME = "Connection";
            const string CALL_RATE_ATTRIBUTE_NAME = "Call-Rate";

            //Load xml
            var doc = new XmlDocument();
            doc.Load(filePath);
            var root = doc.FirstChild;
            var connectionRecordLists = doc.SelectNodes(String.Format("//{0}",CONNECTION_LISTING_NODE_NAME));
            if (connectionRecordLists == null) return;
            for (var i = 0; i < connectionRecordLists.Count; i++)
            {
                var connections = connectionRecordLists[i].SelectNodes(CONNECTION_NODE_NAME);
                if (connections == null) continue;
                for (var j = 0; j < connections.Count; j++)
                {
                    if (connections[j].Attributes != null
                        && connections[j].Attributes[CALL_RATE_ATTRIBUTE_NAME] != null)
                    {
                        Console.WriteLine(connections[j].Attributes[CALL_RATE_ATTRIBUTE_NAME].Value);
                    }

                }
            }
        }

#2


0  

if your xml file has a fixed element and attributes, what about XmlSerializer Class? http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer(v=vs.80).aspx

如果您的xml文件具有固定的元素和属性,那么XmlSerializer类呢? http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer(v=vs.80).aspx