[asp.net]xml 填充组合的框

发布时间: 2017/3/26 5:21:59
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我想要所有的付款方法填补我下拉列表,应该处于一个 xml 文件,我有的付款方法。这应该是方法的 xml 代码︰

    Dim xml As String
    xml = "<?xml version=""1.0"" encoding=""UTF-8""?>"
    xml &= "<gateways ua=""example-php-1.1"">"
    xml &= "<merchant>"
    xml &= " <account>123456</account>"
    xml &= " <site_id>789</site_id>"
    xml &= " <site_secure_code>112233</site_secure_code>"
    xml &= "</merchant>"
    xml &= "<customer>"
    xml &= " <country>NL</country>"
    xml &= " <locale>nl_NL</locale>"
    xml &= "</customer>"
    xml &= " </gateways>"
    Dim apiURl As String

    apiURl = "https://testapi.multisafepay.com/ewx/"


    Dim httpWebRequest As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(apiURl)
    httpWebRequest.Method = "POST"
    httpWebRequest.ContentLength = System.Text.Encoding.UTF8.GetByteCount(xml)
    httpWebRequest.ContentType = "application/x-www-form-urlencoded"

    Dim streamWriter = New System.IO.StreamWriter(httpWebRequest.GetRequestStream())
    streamWriter.Write(xml)
    streamWriter.Close()

    Dim httpWebResponse As System.Net.HttpWebResponse = httpWebRequest.GetResponse()
    Dim streamReader = New System.IO.StreamReader(httpWebResponse.GetResponseStream())
    Dim stringResult = streamReader.ReadToEnd()
    Dim xmlstring As String = stringResult
    Dim xd As System.Xml.XmlDocument = New System.Xml.XmlDocument()
    xd.LoadXml(xmlstring)

stringResult 给这个值︰

<?xml version="1.0" encoding="UTF-8"?>
<gateways result="ok">
<gateways>
<gateway>
<id>IDEAL</id>
<description>iDEAL</description>
</gateway>
<gateway>
<id> MASTERCARD</id>
<description>Visa via Multipay</description>
</gateway>
<gateway>
<id> BANKTRANS</id>
<description> Bank Transfer</description>
</gateway>
<gateway>
<id> VISA</id>
<description> Visa CreditCards</description>
</gateway>
</gateways>
</gateways>

我需要得到之间的值 <id></id> 标签在我 ddlMethod 怎么可以这样?

解决方法 1:

您可以使用 SelectNodes() 方法来获取特定的节点,从 XmlDocument 作为方法参数传递适当的 XPath 字符串。因为 SelectNodes() 返回集合的 XmlNode ,您还需要指定哪些属性 XmlNode 下拉列表控件中显示。在这种情况下,我假设您想要显示的文本之间 <id> 标记所以我们使用 InnerText 属性︰

Dim xd As System.Xml.XmlDocument = New System.Xml.XmlDocument()
xd.LoadXml(xmlstring)
'set data source of dropdown to all <id> elements from XML'
ddlMethod.DataSource = xd.SelectNodes("//id")
ddlMethod.DataTextField = "InnerText"
ddlMethod.DataValueField = "InnerText"
ddlMethod.DataBind()

只有如果在.NET framework 版本中可以使用,我建议要切换到 XDocument 这是更多的现代 XML 库在.NET 中比较老的 XmlDocument 。在各种情况下 XDocument 是更友好比使用 XmlDocument 。示例使用 XDocument 和 LINQ 样式︰

Dim xd As XDocument = XDocument.Parse(xmlstring)
'set data soutce of dropdown to *content* of all <id> elements from XML'
ddlMethod.DataSource = xd.Descendants("id").Select(Function(x) x.Value)
ddlMethod.DataBind()
赞助商