从XML中读取值

时间:2016-06-27 22:17:25

标签: xml vb.net

我有以下XML

#include <iostream>
#include <string>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/bind.hpp>

using namespace std;
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
namespace phx = boost::phoenix;

class Verilog {

  public:
    Verilog();
    ~Verilog();
    void parse(string contents);
    void addModule();

    template <typename Iterator> struct verilog_parser : qi::grammar<Iterator, ascii::space_type> {
        verilog_parser() : verilog_parser::base_type(module) {
            module = qi::eps[phx::bind(&Verilog::addModule, phx::ref(verilog))];
        }

        qi::rule<Iterator, ascii::space_type> module;

        Verilog *verilog;
    };

  private:
    std::vector<int> v_modules;
    verilog_parser<string::const_iterator> m_parser;
};

#include <string>
#include <boost/spirit/include/qi.hpp>

Verilog::Verilog() { m_parser.verilog = this; }
Verilog::~Verilog() {}

void Verilog::parse(string contents) {
    string::const_iterator iter = contents.begin();
    string::const_iterator end = contents.end();
    bool r = phrase_parse(iter, end, m_parser, boost::spirit::ascii::space);
}

void Verilog::addModule() {
    int new_mod = 1;
    v_modules.push_back(new_mod);
}

int main() {
    Verilog verilog;
    string contents = "hello";
    verilog.parse(contents);
}

我正在尝试将SyncServiceToken放入一个字符串中,而我似乎对它失败了。

我目前的代码是:

  <applicationSettings>
<CR.Properties.Settings>
  <setting name="AllowSslTrustAll" serializeAs="String">
    <value>False</value>
  </setting>
  <setting name="SyncInterval" serializeAs="String">
    <value>30</value>
  </setting>
  <setting name="SyncServiceToken" serializeAs="String">
    <value>1597538520147</value>
  </setting>
  <setting name="SyncServiceUrl" serializeAs="String">
    <value>http://cr.myurl.com:4012/-cr/DataService.svc</value>
  </setting>
  <setting name="SaveChangesOptions" serializeAs="String">
    <value>Batch</value>
  </setting>
</CR.Properties.Settings>

1 个答案:

答案 0 :(得分:0)

试试xml linq

Imports System.Xml
Imports System.Xml.Linq
Module Module1

    Sub Main()
        Dim xml As String = _
            "<applicationSettings>" & _
                "<CR.Properties.Settings>" & _
                    "<setting name=""AllowSslTrustAll"" serializeAs=""String"">" & _
                        "<value>False</value>" & _
                    "</setting>" & _
                    "<setting name=""SyncInterval"" serializeAs=""String"">" & _
                        "<value>30</value>" & _
                    "</setting>" & _
                    "<setting name=""SyncServiceToken"" serializeAs=""String"">" & _
                        "<value>1597538520147</value>" & _
                    "</setting>" & _
                    "<setting name=""SyncServiceUrl"" serializeAs=""String"">" & _
                        "<value>http://cr.myurl.com:4012/-cr/DataService.svc</value>" & _
                    "</setting>" & _
                    "<setting name=""SaveChangesOptions"" serializeAs=""String"">" & _
                        "<value>Batch</value>" & _
                    "</setting>" & _
                "</CR.Properties.Settings>" & _
            "</applicationSettings>"

        Dim settings As XElement = XElement.Parse(xml)
        Dim SyncServiceToken As String = settings.Descendants("setting").Where(Function(x) CType(x.Attribute("name"), String) = "SyncServiceToken").FirstOrDefault()
    End Sub

End Module