我有一个XML队列,通过linq读取如下: 示例XML
List<String> PlanInfo = new List<string>();
XDocument xdoc = null;
XNamespace ns = null;
try
{
//List<String> PlanValuesList = new List<string>();
xdoc = XDocument.Load(gLink);
ns = xdoc.Root.Name.Namespace;
var test = (
from planInfo in xdoc.Descendants(ns + "Plan").Descendants(ns + "Eigenschappen")
from Onderdelen in xdoc.Descendants(ns + "Onderdelen")
select new
{
Naam = (string)planInfo.Element(ns + "Naam").Value ?? string.Empty,
Type = (string)planInfo.Element(ns + "Type").Value ?? string.Empty,
Status = (string)planInfo.Element(ns + "Status").Value ?? string.Empty,
Datum = (Convert.ToString(planInfo.Element(ns + "Datum").Value).IndexOf("T") > 0) ? (string)planInfo.Element(ns + "Datum").Value.Substring(0, Convert.ToString(planInfo.Element(ns + "Datum").Value).IndexOf("T")) : (string)planInfo.Element(ns + "Datum").Value ?? string.Empty,
Versie = (string)(planInfo.Element("VersieIMRO") ?? planInfo.Element("VersieGML")) ?? string.Empty,//Convert.ToString(planInfo.Element(ns + "VersieIMRO").Value) ?? String.Empty,
VersiePraktijkRichtlijn = (string)planInfo.Element(ns + "VersiePraktijkRichtlijn").Value ?? string.Empty,
BasisURL = (string)Onderdelen.Attribute("BasisURL")
}).ToList();
foreach (var item in test)
{
PlanInfo.Add(item.Naam);
PlanInfo.Add(item.Type);
PlanInfo.Add(item.Status);
PlanInfo.Add(item.Datum);
PlanInfo.Add(item.Versie);
PlanInfo.Add(item.VersiePraktijkRichtlijn);
PlanInfo.Add(item.BasisURL);
}
}
catch (Exception ex)
{
//Error reading GeleideFormulier link for the plan and manifest
throw;//Just throwing error here, as it is catched in the called method.
}
return PlanInfo;
C#代码
$(document).ready(function() {
Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'));
// Watch for a form submission:
$("#form-submit-btn").click(function(event) {
event.preventDefault();
$('input[type=submit]').prop('disabled', true);
var error = false;
var ccNum = $('#card_number').val(),
cvcNum = $('#card_code').val(),
expMonth = $('#card_month').val(),
expYear = $('#card_year').val();
if (!error) {
// Get the Stripe token:
Stripe.createToken({
number: ccNum,
cvc: cvcNum,
exp_month: expMonth,
exp_year: expYear
}, stripeResponseHandler);
}
return false;
}); // form submission
function stripeResponseHandler(status, response) {
// Get a reference to the form:
var f = $("#new_user");
// Get the token from the response:
var token = response.id;
// Add the token to the form:
f.append('<input type="hidden" name="user[stripe_card_token]" value="' + token + '" />');
// Submit the form:
f.get(0).submit();
}
});
在一些XML文件中&#34; versieIMRO&#34; tag变为&#34; versieGML&#34;,它给出了错误,因为Object Reference未设置为对象的实例。
请让我知道如何检查是否有&#34; versieGML&#34;标签代替&#34; versieIMRO&#34;? 或者,如果其他XML中的标记名称不同,那么如何处理它?</ p>
答案 0 :(得分:2)
您希望将XElement
转换为字符串(而不是XElement.Value
类型的string
属性),以避免在找不到该元素的情况下使用NRE:
Version = (string)x.Element("version"),
如果您想在找不到versionX
元素的情况下使用version
,请尝试以下方式:
Version = (string)(x.Element("version") ?? x.Element("versionX")),
再一次,将Value
属性转换为string
是没用的,它没有任何区别。如果您想获得string.Empty
而不是null
,请再次使用null-coalescing运算符:
Version = (string)(x.Element("version") ?? x.Element("versionX")) ?? string.Empty,