string manip

  • Thread starter Thread starter Aaron
  • Start date Start date
A

Aaron

I need some help writing a function that converts

"name:john"
into
"<name>john</name>"


it should be compatible with different tags, not just names

"name:john"
"email:[email protected]"
into
"<name>john</name>"
"<email>[email protected]</email>"


Thanks in advance
Aaron
 
Here is a simple example;

string sample = "name:john";
string resultString = String.Format("<{0}>{1}</{0}>",sample.Split(':'));

But u should check array lengths, etc. that may cause an exception....
 
Aaron said:
I need some help writing a function that converts

"name:john"
into
"<name>john</name>"

If the point of the exercise is to build well-formed XML, and if you are
going to build any size or complexity of document, it's worth thinking
about using an XMLDocument to do it. It will be slower than string
concatenation with a stringbuilder, but it will ensure that the xlm is
well formed, characters are escaped correctly, etc.

If it's just literally what you wrote above (which looks suspiciously
like homework), look at the using the split method of string, which
breaks a string into an array of shorter strings based on whatever
delimiter you choose. You can then concatenate the strings in the array
using + to get whatever you want.
 
what method do i use to loop through each line?

pesudo code

string ToXml(string input)
{
string resultString = "";

for each line in input
{
resultString = String.Format("<{0}>{1}</{0}>",input.Split(':'));
}
return resultString ;
}

this is what i need to do
the function looks at a line, find whats before the : and then surrounds the
line with the opening and closing xml tags.
 
It is hard to say anything without any input example? What u mean about next
line whole data in a string or u are reading from file.. Please ask again
with a sample data...

--

Thanks,
Yunus Emre ALPÖZEN
BSc, MCAD.NET
 
I'm trying to convert our old contacts database into XML.

If it's SQLServer, you may be as well converting it using the database's
own functionality. Have a look at this:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnanchor
/html/anch_SQLXML.asp

Alternative quick and dirty method: query the database from C# to get a
dataset. You can then create an XmlDataDocument from it, and get the XML
representation using doc.OuterXML.

DataSet ds;
ds = MyMethodReturningADataSet();
XmlDataDocument doc = new XmlDataDocument(ds);
Console.WriteLine(doc.OuterXml);
 
Hello Aaron,

The following snippet is not a valid XML document:
<name>Joe</name>
<email>[email protected]</email>

In order to be a valid document, you need a single root tag:
<mydocument>
<name>Joe</name>
<email>[email protected]</email>
</mydocument>

So, make sure, before you hand in your homework, that you consider the fact
that your data stream may contain more than one set of these values and that
you may need to produce more than one document as a result.
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
 
Back
Top