XML字符串作为参数 [英] XML string as a parameter
问题描述
我有一个像
这样的XML字符串
<根<< PC ID = 1/>< PC ID = 12/<< PC ID = 25/<< PC ID = 29/<< /根
注意:<之间有空格.根> ...< /ROOT>
背后的代码:
Hi,
I have a XML string like
< Root >< PC ID=1 />< PC ID=12 />< PC ID=25 />< PC ID=29 />< /Root >
Note: There are spaces between < ROOT >... < /ROOT >
Codebehind:
<pre lang="xml">protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
Label1.Text = "< Root >";
foreach (ListItem listitem in CheckBoxList1.Items)
{
if (listitem.Selected)
Label1.Text += "< PC ID=" + listitem.Value + " />" ;
}
Label1.Text += "< /Root >";
}
protected string GetAsXMLString(CheckBoxList itemsource)
{
StringBuilder sb = new StringBuilder("<root>");
foreach (ListItem listitem in itemsource.Items)
{
if(listitem.Selected)
sb.Append(String.Format("<PC ID={0}/>", listitem.Value));
}
return sb.ToString();
}
问题:---我的STORED处理程序采用了这样的参数
EXEC [usp_data] @ServerID = 543,@xmlDocument =''< Root>< PC ID ="1"/>< PC ID ="6"/>< PC ID ="12"/> ;< PC ID ="28"/></Root>''
注意:-< root>< pc id ="1">< pc id ="6">< pc id ="12">< pc id ="28之间没有空格>
如何生成没有空格的字符串...如果我在c#代码中删除空格,则无法构建该字符串...
ISSUE:--- my STORED proc take the paramete like this
EXEC [usp_data] @ServerID = 543, @xmlDocument = ''<Root><PC ID="1"/><PC ID="6"/><PC ID="12"/><PC ID="28"/></Root>''
Note :-- there are no spaces between <root><pc id="1"><pc id="6"><pc id="12"><pc id="28">
How to generate the string with out spaces ...If i remove the spaces in my c# code am unable to build that string...
推荐答案
我不确定实际的问题是什么,但是如果您不按原样手动构建XML,也许会有所帮助.
我建议使用 XElement [ ^ ]和 XDocument [
I am not sure what your actual issue is, but maybe it will help if you do not build the XML manually as you are.
I would recomend using objects like XElement[^] and XDocument[^].
Then when you need the string format you can just ToString it.
Is your issue that you are adding <root> but not closing it? I am seeing the StringBuilder initialized with it but there is no closing.
Again, using an XML object would help you in this matter. It handles closing tags and proper placing of the attributes and child elements.
目前尚不清楚为什么不能在C#
中生成没有空格的XML string
,并且似乎在问题中给出的代码将生成XML string without spaces
.
此外, Collin Jasnoch 给出的解决方案1也可以用于生成不带空格的XML字符串.
但是,如果有specific requirement
可以从XML字符串中删除空格,则可以使用Regex
类的Replace
方法用Empty string
替换空格,如下所示:
It is not clear whyXML string
without spaces cannot be generated inC#
and it appears that the second block of code given in the question generates theXML string without spaces
.
Further the Solution 1 given by Collin Jasnoch can also be used to generate XML string without spaces.
However, if there is aspecific requirement
to remove spaces from the XML string, thenReplace
method ofRegex
class can be used to replace the spaces withEmpty string
as shown below:
string xmlString = @"< Root >< PC ID=1 / >< PC ID=12 / >< PC ID=25 / >< PC ID=29 / >< / Root >";
string xmlStringSansSpaces = Regex.Replace(xmlString,@"(?<=(?:<|/))\s+|\s+(?=(?:>))",
string.Empty, RegexOptions.CultureInvariant);
Console.WriteLine (xmlStringSansSpaces);
//Output
//<Root><PC ID=1 /><PC ID=12 /><PC ID=25 /><PC ID=29 /></Root>
在上面的代码中,模式(?<=(?:<|/))\s+|\s+(?=(?:>))
搜索whitespace
或< or /
或>
,但捕获only the whitespace
并替换为Empty
字符串.
In the above code, the pattern (?<=(?:<|/))\s+|\s+(?=(?:>))
searches for whitespace
either preceded by < or /
or followed by >
but captures only the whitespace
which is replaced with an Empty
string.
我猜字符串:应该包含在您的字符串中!
i guess string: should be containted in your string!
这篇关于XML字符串作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!