0

I am given the string that looks like this

<States>
  <State>
    <State_ID>1</State_ID>
    <Job>
      <Job_ID>2</Job_ID><Name>Walk</Name>
    </Job>
  </State>
</States>

I am also trying to add another string to this such as

<State>
  <State_ID>2<State_ID>
  <Job>
    <Job_ID>9</Job_ID><Name>Sprint</Name>
  </Job>
</State>

So is there a way to append these strings together to look like this

<States>
  <State>
    <State_ID>1</State_ID>
    <Job>
      <Job_ID>2</Job_ID><Name>Walk</Name>
    </Job>
  </State>
  <State>
    <State_ID>2<State_ID>
    <Job>
      <Job_ID>9</Job_ID><Name>Sprint</Name>
    </Job>
  </State>
</States>

Again, these are strings in C# Visual Studio formatted like xml but not xml files

4
  • 3
    1) use xml parser like XDocument to load xml 2) use facilities of said parser to add required xml 3) save Commented May 25, 2017 at 20:27
  • @ Ed Plunkett I had similar thinking to you. The only prob;lem is the </States> at the end. Is there a way to remove just that concatenate the strings and add that back on the end? Commented May 25, 2017 at 20:30
  • @Will coment is th way to go. But if insist in your way, use substring to pick he first part, concatenate with the second string and then use substring again to pick the last part (if the string is not static, if it's static just concatenate too) Commented May 25, 2017 at 20:38
  • Yeah if you have a specific format and powerful tools to handle it baked into the framework, don't brute force it with string concatenation. If this will only ever be used once and not need to be maintained, sure - but if you have to maintain it avoid string manipulation if possible (as @Will noted). The answer from Alexander Petrov has a very short and elegant implementation of this which potentially uses less lines of code than string manipulation would even be. Commented May 25, 2017 at 23:55

4 Answers 4

3

Use linq2xml is very easy.

string text1 = @"
<States>
  <State>
    <State_ID>1</State_ID>
    <Job>
      <Job_ID>2</Job_ID><Name>Walk</Name>
    </Job>
  </State>
</States>";

string text2 = @"
<State>
  <State_ID>2</State_ID>
  <Job>
    <Job_ID>9</Job_ID><Name>Sprint</Name>
  </Job>
</State>";

var xml1 = XElement.Parse(text1);
var xml2 = XElement.Parse(text2);

xml1.Add(xml2);

//Console.WriteLine(xml1);
//xml1.Save("states.xml");

xml1 contains result.

Sign up to request clarification or add additional context in comments.

1 Comment

Well, this is quite a beautiful solution. I'll leave mine here anyway, as alternatives never hurt.
1

Using xml linq in c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string xml1 =
                "<States>" +
                   "<State>" +
                     "<State_ID>1</State_ID>" +
                     "<Job>" +
                       "<Job_ID>2</Job_ID><Name>Walk</Name>" +
                     "</Job>" +
                   "</State>" +
                 "</States>";

            string xml2 =
                "<State>" +
                  "<State_ID>2</State_ID>" +
                  "<Job>" +
                    "<Job_ID>9</Job_ID><Name>Sprint</Name>" +
                  "</Job>" +
                "</State>";


            XElement states = XElement.Parse(xml1);
            XElement newState = XElement.Parse(xml2);


            states.Add(newState);
        }
    }
}

Comments

0

Well, you can easily combine them:

var string1 = "<States>\r\n  <State>\r\n    <State_ID>1</State_ID>\r\n    <Job>\r\n      <Job_ID>2</Job_ID><Name>Walk</Name>\r\n    </Job>\r\n  </State>\r\n</States>";
var string2 = "<State>\r\n  <State_ID>2</State_ID>\r\n  <Job>\r\n    <Job_ID>9</Job_ID><Name>Sprint</Name>\r\n  </Job>\r\n</State>";
var result = $"{string1}\n{string2}";

Of course there are other ways to combine strings, like:

var result = string.Concat(new[] {string1, "\n", string2});

However, if you want to include the second part into the state of the first string, it'd look a little bit different:

var string1 = "<States>\r\n  <State>\r\n    <State_ID>1</State_ID>\r\n    <Job>\r\n      <Job_ID>2</Job_ID><Name>Walk</Name>\r\n    </Job>\r\n  </State>\r\n</States>";
var string2 = "<State>\r\n  <State_ID>2</State_ID>\r\n  <Job>\r\n    <Job_ID>9</Job_ID><Name>Sprint</Name>\r\n  </Job>\r\n</State>";
var result = $"{string.Join("", string1.Split('\n').Take(string1.Length-1))}\n{string2}\n{string1.Split('\n').LastOrDefault()}";

Now it takes the last line, removes it from the first part and adds it to the last part. The formatting isn't optimal now, but I doubt this is relevant.

However, if you want to do it as you should do it, this is, with XmlDocuments, you could use this:

var string1 = "<States>\r\n  <State>\r\n    <State_ID>1</State_ID>\r\n    <Job>\r\n      <Job_ID>2</Job_ID><Name>Walk</Name>\r\n    </Job>\r\n  </State>\r\n</States>";    //Your first xml string
var string2 = "<State>\r\n  <State_ID>2</State_ID>\r\n  <Job>\r\n    <Job_ID>9</Job_ID><Name>Sprint</Name>\r\n  </Job>\r\n</State>";    //Your second xml string
var document1 = new XmlDocument();    //Create XmlDocuments for your strings
document1.LoadXml(string1);
var document2 = new XmlDocument();
document2.LoadXml(string2);
var node = document1.ImportNode(document2.DocumentElement, true);    //Import the second document as a node into the first one
document1.LastChild.AppendChild(node);    Add the node to the last node of the first document
string result;    //Write the first document into the result string
using (var stream = new MemoryStream())
using (var xmlTextWriter = new XmlTextWriter(stream, Encoding.Unicode))
{
    xmlTextWriter.Formatting = Formatting.Indented;    //Set the format to indented, so that the result looks more beautiful

    document1.WriteContentTo(xmlTextWriter);
    stream.Flush();
    xmlTextWriter.Flush();
    stream.Position = 0;

    result = new StreamReader(stream).ReadToEnd();
}

This takes the strings, creates documents from them, imports the second one into the first one, adds it to the last node of the first document (that is the States node) and converts it to a string again.

Here you can read more about XmlDocuments.

Comments

-1

It's easy to solve this problem with Replace, it's much faster than to load a XML parser. If you have more complex XML Strings - maybe you will need one - and if you have bigger and more strings, then it's better to use StringBuilder.Append() instead of concating with +:

string text1 = @"
<States>
    <State>
        <State_ID>1</State_ID>
        <Job>
            <Job_ID>2</Job_ID><Name>Walk</Name>
        </Job>
    </State>
</States>";

string text2 = @"
    <State>
        <State_ID>2</State_ID>
        <Job>
            <Job_ID>9</Job_ID><Name>Sprint</Name>
        </Job>
    </State>";

string replace = Environment.NewLine + "</States>";
string text = (text1 + text2).Replace(replace, "") + replace;

text contains the result.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.