【问题标题】:Read node from XML file从 XML 文件中读取节点
【发布时间】:2018-01-29 11:23:26
【问题描述】:

对我在这里做错了什么有点困惑。我的 XML

<?xml version="1.0" encoding="utf-8"?>
<customers xmlns="http://example.com/ns/" si="0" records="2">
  <customer id="123456789">
    <dob>2017-12-10T16:22:27.033Z</dob>
    <location>
      <number>444555666777</number>
    </location>
    <link rel="self" href="http://example.com" />
  </customer>
  <customer id="987654321">
    <dob>2017-12-11T17:00:00.033Z</dob>
    <location>
      <number>555666999888</number>
    </location>
    <link rel="self" href="http://example.com" />
  </customer>
  <link rel="self" href="http://example.com" />
</customers>

我正在尝试从文件中获取这两个记录(客户)以及其中包含的数据(最终我会将其更改为 LoadXml,因为目标 XML 来自 URL,但为了测试我已经复制到文件中离线工作)

所以我的代码

    Dim Xd As XmlDocument = New XmlDocument
    Xd.Load("C:\XMLFile1.xml")
    Dim Total As Integer = Xd.DocumentElement.Attributes("records").Value

    If Total > 0 Then
    Dim Customer = Xd.SelectSingleNode("/customers/customer")

客户总是一无所有?我尝试了几种变体,包括

Xd.SelectSingleNode("/customers")
Xd.SelectSingleNode("/customer")

我尝试过 Xd.DocumentElement,但我认为这也不正确。

我哪里出错了? google了一下,很多例子都用的和上面一样?

编辑

我决定看看 Linq to XML,这就是我所拥有的

    Dim X As XElement = Xelement.Load("C:\XMLFile1.xml")
    Dim Total = Xelement.Attribute("records").Value
    Dim Customers As IEnumerable(Of XElement) = Xelement.Elements()

    ' Read the entire XML
    For Each c In Customers
        Console.WriteLine(c)
    Next

看起来不错,但我不知道是否还有其他需要添加的内容,或者是否有更好的方法。

【问题讨论】:

  • 我强烈怀疑命名空间在这里引起了问题。虽然您可以使用 XmlDocument 来完成,但我强烈建议使用 LINQ to XML(XDocument、XElement 等),因为它使命名空间的处理更加简单。
  • 上帝说话了……
  • 我已经按照@JonSkeet 的建议做了,看起来它已经解决了。由于没有提出答案并且只有方向,我无法将其标记为答案,但我已经编辑了我的帖子以显示我做了什么,以防万一有人在将来越过这个线程。很高兴您添加答案,我会接受
  • 您可以自己添加您的编辑作为答案,然后将其标记为答案。您甚至可以在答案中归功于@JonSkeet。 :-)

标签: .net xml vb.net


【解决方案1】:

这是将 xml 加载到对象中的一种方法

先定义符合xml格式的对象

[XmlRoot("customers", Namespace = "http://example.com/ns/")]
public class Customers
{
    [XmlAttribute("records")]
    public int Records { get; set; }

    [XmlElement("customer")]
    public Customer[] CustomerArray;
}

public class Customer
{
    [XmlAttribute("id")]
    public int Id { get; set; }

    [XmlElement("dob")]
    public DateTime Dob { get; set; }

    [XmlElement("location")]
    public Location Location { get; set; }

    [XmlElement("link")]
    public Link Link { get; set; }
}

public class Location
{
    [XmlElement("number")]
    public long Number { get; set; }
}

public class Link
{
    [XmlAttribute("rel")]
    public string Rel { get; set; }
    [XmlAttribute("href")]
    public string HRef { get; set; }
}

然后反序列化如下

System.Xml.Serialization.XmlSerializer serializer11 = new System.Xml.Serialization.XmlSerializer(typeof(Customers));
Customers obj11 = (Customers)serializer11.Deserialize(XElement.Load(/* YOUR XML FILE PATH */).CreateReader());

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-29
    • 1970-01-01
    • 1970-01-01
    • 2013-08-17
    • 2013-12-19
    • 1970-01-01
    • 2018-02-22
    相关资源
    最近更新 更多