Click here to Skip to main content
15,901,283 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
<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();
    }

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...
Posted
Updated 6-Jun-12 5:57am
v3
Comments
Tim Corey 6-Jun-12 11:25am    
Why are you unable to build the string without the spaces? Do you get an error? If so, what error? If not, what stops you?

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.

[Edit]
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.
 
Share this answer
 
v5
Comments
lewax00 6-Jun-12 12:12pm    
Good suggestion. +5
[no name] 6-Jun-12 12:17pm    
Thank you
VJ Reddy 6-Jun-12 22:54pm    
Good answer. 5!
[no name] 7-Jun-12 11:33am    
Thank you
It is not clear why XML string without spaces cannot be generated in C# and it appears that the second block of code given in the question generates the XML 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 a specific requirement to remove spaces from the XML string, then Replace method of Regex class can be used to replace the spaces with Empty string as shown below:
C#
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>

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.
 
Share this answer
 
Comments
myansweris 8-Jun-12 11:18am    
got it ...Thankyou...
VJ Reddy 9-Jun-12 0:32am    
You are welcome and thank you for accepting the solution :)
i guess string: should be containted in your string!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900