从 Web 读取 XML 并显示内容

发布于 2024-11-27 06:25:05 字数 659 浏览 1 评论 0原文

我正在从网络上读取一个文件:

<?xml version='1.0' encoding='UTF-8'?>
<eveapi version="2">
  <currentTime>2011-07-30 16:08:53</currentTime>
  <result>
    <rowset name="characters" key="characterID" columns="name,characterID,corporationName,corporationID">
      <row name="Conqrad Echerie" characterID="91048359" corporationName="Federal Navy Academy" corporationID="1000168" />
    </rowset>
  </result>
  <cachedUntil>2011-07-30 17:05:48</cachedUntil>
</eveapi>

我对 XML 还很陌生,我看到有很多方法可以读取 XML 数据,我是否想要某种特定的方法来执行此操作?我想要做的是将所有数据加载到 StreamReader 中?然后使用获取;放;稍后提取数据?

I'm reading a file like from the web:

<?xml version='1.0' encoding='UTF-8'?>
<eveapi version="2">
  <currentTime>2011-07-30 16:08:53</currentTime>
  <result>
    <rowset name="characters" key="characterID" columns="name,characterID,corporationName,corporationID">
      <row name="Conqrad Echerie" characterID="91048359" corporationName="Federal Navy Academy" corporationID="1000168" />
    </rowset>
  </result>
  <cachedUntil>2011-07-30 17:05:48</cachedUntil>
</eveapi>

im still new to XML and i see there are many ways to read XML data, is there a certain way im going to want to do this? what i want to do is load all the data into a StreamReader? and then use get; set; to pull the data later?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陪你搞怪i 2024-12-04 06:25:05

如果您想要基于对象的访问,请将示例 xml 放入文件中并运行它,

xsd.exe my.xml
xsd.exe my.xsd /classes

这将创建 my.cs,它是一个类似于可与 XmlSerializer 一起使用的 xml 的对象模型:

var ser = new XmlSerializer(typeof(eveapi));
var obj = (eveapi)ser.Deserialize(source);

If you want object-based access, put the example xml in a file and run

xsd.exe my.xml
xsd.exe my.xsd /classes

this will create my.cs which is an object model similar to the xml that you can use with XmlSerializer:

var ser = new XmlSerializer(typeof(eveapi));
var obj = (eveapi)ser.Deserialize(source);
ㄖ落Θ余辉 2024-12-04 06:25:05

如果您需要以简单的方式使用数据,尤其是当您刚接触 XML 时,请使用 XmlDocument
加载文档:

using System.Xml;
using System.IO;
public class someclass {
    void somemethod () {
        //Initiate the XmlDocument object
        XmlDocument xdoc;
        //To load from file
        xdoc.Load("SomeFolder\\SomeFile.xml");
        //Or to load from XmlTextReader, from a file for example
        FileStream fs = FileStream("SomeFolder\\SomeFile.xml", FileMode.Open, FileAccess.Read);
        XmlTextReader reader = new XmlTextReader(fs);
        xdoc.Load(reader);
        //In fact, you can load the stream directly
        xdoc.Load(fs);

        //Or, you can load from a string
        xdoc.LoadXml(@"<rootElement>
                    <element1>value1</element1>
                    <element2>value2</element2>
                    </rootElement>");
    }
}

我个人发现使用 XmlDocument 来导航 Xml 文件要容易得多。

为了有效地使用它,您需要学习 XPath。例如,要获取第一个 row:

string name = xdoc.SelectSingleNode("/eveapi/result/rowset/row").Attribute["name"].InnerText;

或更多 XPath: 的名称,

string name = xdoc.SelectSingleNode("/eveapi/result/rowset/row/@name").InnerText;

您甚至可以使用 filter:

XmlNodeList elems = xdoc.SelectNodes("//*[@name=\"characters\"]") 

为您提供 rowset 元素。

但这不是主题。

If you need to use the data in the easy way, especially when you're new to XML, use XmlDocument.
To load the document:

using System.Xml;
using System.IO;
public class someclass {
    void somemethod () {
        //Initiate the XmlDocument object
        XmlDocument xdoc;
        //To load from file
        xdoc.Load("SomeFolder\\SomeFile.xml");
        //Or to load from XmlTextReader, from a file for example
        FileStream fs = FileStream("SomeFolder\\SomeFile.xml", FileMode.Open, FileAccess.Read);
        XmlTextReader reader = new XmlTextReader(fs);
        xdoc.Load(reader);
        //In fact, you can load the stream directly
        xdoc.Load(fs);

        //Or, you can load from a string
        xdoc.LoadXml(@"<rootElement>
                    <element1>value1</element1>
                    <element2>value2</element2>
                    </rootElement>");
    }
}

I personally find XmlDocument far easier to use for navigating an Xml file.

To use it efficiently, you need to learn XPath. For example, to get the name of the first row:

string name = xdoc.SelectSingleNode("/eveapi/result/rowset/row").Attribute["name"].InnerText;

or even more XPath:

string name = xdoc.SelectSingleNode("/eveapi/result/rowset/row/@name").InnerText;

you can even filter:

XmlNodeList elems = xdoc.SelectNodes("//*[@name=\"characters\"]") 

gives you the rowset element.

But that's off topic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文