【问题标题】:Read a String as XML in .NET 2.0在 .NET 2.0 中将字符串读取为 XML
【发布时间】:2011-03-04 17:17:28
【问题描述】:

我有一个来自 DB 的 string。我想将其阅读为XML。字符串如下所示

<settings>
  <setting name="OfferIDs" value="47,48,49,50,51,52,53,76,77,78,79" />
  <setting name="someothersetting" value="" />
  <setting name="anothersetting" value="" />
</settings>

我想使用 VB.NET 将 OfferIDs 的值作为字符串获取。非常感谢。

【问题讨论】:

    标签: xml vb.net .net-2.0


    【解决方案1】:

    编辑:要使用 .NET 2,我可能会使用 XmlDocument

    Dim document = new XmlDocument()
    document.LoadXml(xml)
    

    然后,您需要在文档中导航,通过其name 属性查找适当的元素,然后获取该元素的value 属性。这些天我对XmlDocument 生疏了,但希望这足以让你开始......


    最简单的方法可能是用 LINQ to XML 加载它:

    Dim settings = XElement.Parse(xml)
    

    ... 然后查询它。在 C# 中这很容易,但我的 VB-fu 在 LINQ 查询部分失败了。

    C# 类似于:

    XElement settings = XElement.Parse(xml);
    string offerIds = settings.Elements("setting")
                              .Where(x => (string) x.Attribute("name") == "OfferIDS")
                              .Select(x => (string) x.Attribute("value"))
                              .Single();
    

    【讨论】:

    • 很遗憾不能使用 LINQ,因为解决方案是在 .NET 2.0 上,但非常感谢您的回复
    • @Chin:以后在问题中值得一提。考虑到它已经发布了多长时间,我倾向于假设这些天至少有 .NET 3.5。正在编辑...
    • 对此很抱歉.. 以后会做的。
    • @Chin:没问题。我已经留下了原始答案,以防将来有人需要它,但我编辑的答案的顶部应该可以帮助你在 .NET 2 中。
    【解决方案2】:

    如果无法访问 LINQ,您的代码将如下所示...

    Dim xmlDoc As New System.Xml.XmlDocument
    xmlDoc.Load("YourXmlFile.xml")
    Dim OfferIDs As String = xmlDoc.SelectSingleNode("//settings/setting[@name='OfferIDs']").Attributes("value").Value
    

    这应该可以满足您的需求。

    【讨论】:

      【解决方案3】:

      您可以使用StringReader 结合XmlReader(对XML 数据的快速、非缓存、只进访问)从字符串中读取xml。 在 C# 中:

      string source = GetXmlAsString ();
      using (StringReader xml = new StringReader (source)) {
          using (XmlReader reader = XmlReader.Create (xml)) {
              while (reader.Read ()) {
                  if (reader.IsStartElement ("setting")) {
                      string attrName = reader.GetAttribute ("name");
                      if (attrName == "OfferIDs") {
                          string attrValue = reader.GetAttribute ("value");
                      }
                  }
              }
          }
      }
      

      在 VB 中:

      Dim source As String = GetXmlAsString()
      Using xml As New StringReader(source)
          Using reader As XmlReader = XmlReader.Create(xml)
              While reader.Read()
                  If reader.IsStartElement("setting") Then
                      Dim attrName As String = reader.GetAttribute("name")
                      If attrName = "OfferIDs" Then
                          Dim attrValue As String = reader.GetAttribute("value")
                      End If
                  End If
              End While
          End Using
      End Using
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-24
        • 2019-04-14
        • 2012-10-19
        • 2014-01-16
        • 2021-05-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多