我有:带有一些元素的XML.可以在此XML中定义的子元素,也可以不在此XML中定义.需要在子元素存在时提取子元素的值.
如何在不抛出对象引用错误的情况下获取值?
例如:
string sampleXML = "<Root><Tag1>tag1value</Tag1></Root>";
//Pass in <Tag2> and the code works:
//string sampleXML = "<Root><Tag1>tag1value</Tag1><Tag2>tag2Value</Tag2></Root>";
XDocument sampleDoc = XDocument.Parse(sampleXML);
//Below code is in another method, the 'sampleDoc' is passed in. I am hoping to change only this code
XElement sampleEl = sampleDoc.Root;
string tag1 = String.IsNullOrEmpty(sampleEl.Element("Tag1").Value) ? "" : sampleEl.Element("Tag1").Value;
//NullReferenceException:
//Object reference not set to an instance of an object.
string tag2 = String.IsNullOrEmpty(sampleEl.Element("Tag2").Value) ? "" : sampleEl.Element("Tag2").Value;
Run Code Online (Sandbox Code Playgroud) .net c# exception-handling linq-to-xml nullreferenceexception
我有一个看起来像这样的对象:
[
{'el': 123},
{'el': 234},
{'el': 345}
]
Run Code Online (Sandbox Code Playgroud)
我想将其转换为仅包含值的数组,并删除内部的额外"el":
var myArray = [ 123, 234, 345];
Run Code Online (Sandbox Code Playgroud)
有没有简单的方法可以做到这一点,而不使用JSON.parse或其他JSON友好方法?老式的Javascript是我正在寻找的.
我有一个'nvarchar(max)'类型的列,它现在应该包含XML信息而不仅仅是一个字符串.
说:col1的值为'abc'现在它有值,附加信息:
<el1>abc</el2>
<el2>someotherinfo</el2>
Run Code Online (Sandbox Code Playgroud)
将信息存储到列中很好,因为它仍然可以作为字符串推入.但是,提取相同的信息并使用/替换来自此列的相同信息'abc'正在其他表中的各种其他连接中使用,这是我无法弄清楚的.
如果来自另一个表的值'abcd'而不丢失其他信息,我怎么能将这些信息输入abcd?
我正在从应用程序端构建XML并在nvarchar()类型的列中更新它.所有列都已被替换为保存XML,因此安全的假设是col1只保存类似于上面提到的XML.按原样推送XML,它工作正常.但是,我应该如何提取信息以在连接中使用它?
如何从此nvarchar()字符串中提取特定元素以在连接中使用它?以前,此列"Col1"仅用作字符串,并且检查是这样完成的:
where tablex.colx = table1.col1
Run Code Online (Sandbox Code Playgroud)
或更新Table2在哪里
XML:
<Root>
<Elements>
<Element>el1</Element>
<Element>el2</Element>
</Elements>
<Elements>
<Element>el1</Element>
<Element>el2</Element>
</Elements>
</Root>
Run Code Online (Sandbox Code Playgroud)
尝试生成为同一元素应用两个不同的模板.
主要模板:
<xsl:stylesheet version="1.0">
<xsl:template match="/Root">
At root level
<xsl:apply-templates select="Elements">
<h1>Render something more</h1>
<xsl:apply-templates select="Elements" mode="1:Custom">
</xsl:template>
<!-- This doesn't render though it is called above-->
<xsl:template match="Elements">
render something here
</xsl:template>
<!-- This renders twice -->
<xsl:template match="Elements" mode="1:Custom">
render something else here
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
如果我将模式添加到第一个模板,则两者都不会渲染.
还尝试过:
<xsl:apply-templates select="Elements" mode="1:Custom" />
Run Code Online (Sandbox Code Playgroud)
使用不同的模板应用为:
<xsl:apply-templates select="Elements" mode="Different" />
Run Code Online (Sandbox Code Playgroud)
只有两个中的一个(第一个具有指定模式的渲染).即
<xsl:template match="Elements">
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
没有呈现
或
<xsl:template match="Elements" mode="Different" />渲染两次. …
此代码无法正常工作。它没有在span.day内显示任何文本,而应该在今天(显示本文的星期二)显示该文本。它还没有在$.each回调中正确添加类“ currentDay” 。
$('#showLess span.day').innerHTML=weekday[today];
$.each($('#showMore p span.day'), function(index, item) {
if(typeof item.innerHTML != 'undefined')
{
alert('item.text:' +item.innerHTML);
alert('weekday[today]'+item.innerHTML.indexOf(weekday[today]));
if(item.innerHTML.indexOf(weekday[today])!=-1) {
alert("check which element has added class currentDay:");
item.addClass('currentDay');
}else{
if(item.hasClass('currentDay')) {
item.removeClass('currentDay');
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
.innerHTML 未更改HTML,未按预期添加其他类。
<p id="showLess" class="less">
<span class="day">**Tuesday**</span>
</p>
Run Code Online (Sandbox Code Playgroud)
为什么没有显示一天?
为什么显示/隐藏不起作用?
$('.less').on('click', function(e) {
$('#showMore').show();
$('#showLess').hide();
});
$('.more').bind('click', function(e) {
$('#showLess').show();
$('#showMore').hide();
});
Run Code Online (Sandbox Code Playgroud) javascript ×2
xml ×2
.net ×1
arrays ×1
c# ×1
innerhtml ×1
jquery ×1
linq-to-xml ×1
sql ×1
sql-server ×1
t-sql ×1
templates ×1
xslt ×1