当前位置:网站首页>C reads XML on the web

C reads XML on the web

2022-06-25 07:44:00 ※※ Bingxin ※※

One 、 Use LINQ Read
Use Xdocument Upper Load Method , Can quickly load a XML file , And then use LINQ Yes load XML Document for query or other operations , Here is only a simple partial calendar . therefore , Once a set of elements is queried, a set of elements is returned , You can use a simple foreach Loop through each element . The core code is as follows :

/// <summary>

    ///  Use LINQ Read web Upper xml

    /// </summary>

    public static void UseLINQ()

    {

        string sURL = "http://localhost:9058/GameServerInfo/XMLFile.xml";

        XDocument oXDoc = XDocument.Load(sURL);

        var qurey = from e in oXDoc.Descendants()

                    where e.NodeType == XmlNodeType.Element

                    select new

                    {

                        ElementName=e.Name.ToString(),

                        ElementValue=e.Value

                    };

        foreach(var elementInfo in qurey)

        {

            HttpContext.Current.Response.Write(string.Format("ElementName->{0} ElementValue->{1}<br />", elementInfo.ElementName, elementInfo.ElementValue));

        }

    }

Two 、 Use XmlReader Constructors
Of course, you can also use the URL Of XmlReader.Create Method to complete the operation , It uses a XmlUrlResolver Class to detect the incoming URL, Then open a path to the URL Representative XML The document flow . To specify the settings on the reader , You can use another Create overloaded , It's also through a XmlReaderSetting Examples accomplish this . The code is as follows :

/// <summary>

    ///  Use XmlReader Constructors 

    /// </summary>

    public static void UseXmlReader()

    {

        string sURL = "http://localhost:9058/GameServerInfo/XMLFile.xml";

        using(XmlReader read=XmlReader.Create(sURL))

        {

            while(read.Read())

            {

                switch (read.NodeType)

                {

                    case XmlNodeType.Element:

                        HttpContext.Current.Response.Write(string.Format("ElementName->{0} <br />", read.Name));

                        break;

                    case XmlNodeType.Text:

                        HttpContext.Current.Response.Write(string.Format("ElementValue->{0}<br />", read.Value));

                        break;

                    case XmlNodeType.CDATA:

                        HttpContext.Current.Response.Write(string.Format("ElementValue->{0}<br />", read.Value));

                        break;

                        //other

                }

            }

        }

    }

原网站

版权声明
本文为[※※ Bingxin ※※]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206250541235663.html