Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am having an string like,

XML
<root>
<name>
asdf
</name>
<age>
21
</age>
<city>
kjhgf
</city>
<root>


I need to add a string inside the tags like,

XML
<root>
<name "htmltag">
asdf
</name>
<age "html tag">
21
</age>
<city "html tag">
kjhgf
</city>
</root>
Posted
Updated 1-Dec-14 19:02pm
v3
Comments
Nathan Minier 14-Nov-14 7:00am    
That's an attribute, not to be confused with a 'string inside a node', which would be data as opposed to metadata.

What library are you using, what have you tried, and what issues are you having?
riodejenris14 14-Nov-14 7:11am    
Yes have to update the attribute from "name" to "name "html"". This is my requirement share your thoughts to do the task.
Sinisa Hajnal 14-Nov-14 7:24am    
What have you tried? There are good documented XML manipulation classes in .NET. What is the problem?
Richard Deeming 14-Nov-14 8:53am    

C#
try
            {
                string _strline = "<ul>This is ul</ul><li>This is li</li><b>This is bold</b>";
                string append = "xmlns=" + "\" http://www.w3.org/1999/xhtml\"";
                string result = string.Empty;
                var arr = Regex.Matches(_strline, @"<[A-Za-z-']+>").Cast<Match>().Select(m => m.Value).ToArray();
                var openTags = arr.Distinct().ToArray();
                result = _strline;
                for (int i = 0; i < openTags.Length; i++)
                {
                    var temp = openTags[i].Replace(">", " " + append + " >");
                    result = result.Replace(openTags[i], temp);

                }
                Console.WriteLine(_strline);
                Console.WriteLine(result);
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                throw ex;
            }
 
Share this answer
 
So your need should lead to questions about things you don't know...
You didn't do that, asked no questions, so I ask.

You know the difference between the XML-Schema and the data?
You want to add an attribute to the schema or just change an attributes data?
Which tech you use for read write the XML and what's the problem you have with using them?
...

you see it's a QUESTION and ANSWERS Forum ;-)
 
Share this answer
 
Comments
riodejenris14 2-Dec-14 1:08am    
May be I posted wrong, but I rectified and updated the question. I think you get confused between XML and a string. Don't be in seventh heaven that you were well known in tech. Try to think smart out of box and not in hackneyed views.
If it is a question forum just answer for it, don't explore other views and thoughts which you don't have knowledge.
If you can resolve do else skip, it is a best practise of a good techy guy.
Anyhow thanks for your comment.
johannesnestler 2-Dec-14 9:30am    
Yea sorry for sounding a little angry in my "solution" it was meant as a "funny hint" how to make a better question. I'm not shure what you mean with your statements about tech... Fact is your post didn't (and doesn't) state any questions. I think it's best practice to formulate an actual question if you want to ask for some help. Don't repost and google or explain tech "words" you don't know. You should have better answered the commenters questions (or mine) - if you would have answered my questions, I would have tried to help... I think explaining a problem to others is a valuable skill for every programmer

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