Reputation: 33
I have xml data in a string and i want it to split and i want to display the result in a Lable.
Here is my code:
string param = <HCToolParameters><BatchId>12</BatchId><HCUser>Admin</HCUser</HCToolParameters>;
var a = param.Split(new string[] { "<HCToolParameters>" }, StringSplitOptions.RemoveEmptyEntries);
var b = param.Split(new string[] { "<BatchId>12</BatchId>" }, StringSplitOptions.RemoveEmptyEntries);
var c = param.Split(new string[] { "<HCUser>Admin</HCUser>" }, StringSplitOptions.RemoveEmptyEntries);
var d = param.Split(new string[] { "</HCToolParameters>" }, StringSplitOptions.RemoveEmptyEntries);
Example:
String value =
<HCToolParameters><BatchId>12</BatchId><HCUser>Admin</HCUser></HCToolParameters>
Expected Result:
<HCToolParameters>
<BatchId>12</BatchId>
<HCUser>Admin</HCUser>
</HCToolParameters>
Upvotes: 1
Views: 50
Reputation: 18127
From what I see in the begging you have valid xml so, stop spliting it and use Xml Parser !
string param =@"<HCToolParameters><BatchId>12</BatchId><HCUser>Admin</HCUser></HCToolParameters>";
XDocument doc = XDocument.Parse(param);
Console.WriteLine(doc.ToString());
Upvotes: 1
Reputation: 14044
You can use XmlTextWriter.Formatting = Formatting.Indented;
because what is see is, you wanted to format your XML string. This function might do the trick for you
public static String FormatMyXML(String SomeXML)
{
String Result = "";
MemoryStream mStream = new MemoryStream();
XmlTextWriter wrtr = new XmlTextWriter(mStream, Encoding.Unicode);
XmlDocument document = new XmlDocument();
try
{
document.LoadXml(SomeXML);
wrtr.Formatting = Formatting.Indented;
document.WriteContentTo(wrtr);
wrtr.Flush();
mStream.Flush();
mStream.Position = 0;
StreamReader sReader = new StreamReader(mStream);
String FormattedXML = sReader.ReadToEnd();
Result = FormattedXML;
}
catch (XmlException)
{
}
mStream.Close();
wrtr.Close();
return Result;
}
Upvotes: 0
Reputation: 509
Well you could do it easy by this:
value = value.Replace("><", ">" + Environment.NewLine + "<");
This would work out in your example and is easy,... if you need it as Array (I don't realy know why you would try it this way:
var array = value.Replace("><", ">#<").Split('#');
Upvotes: 0