将字符串附加到格式为xml c#的字符串中

时间:2017-05-25 20:24:59

标签: c# xml string

我收到的字符串看起来像这样

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

我也在尝试添加另一个字符串,例如

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

那么有没有办法将这些字符串追加到一起看起来像这样

<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>

同样,这些是C#Visual Studio中的字符串格式化为xml但不是xml文件

4 个答案:

答案 0 :(得分:3)

使用linq2xml非常简单。

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包含结果。

答案 1 :(得分:1)

在c#

中使用xml linq
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);
        }
    }
}

答案 2 :(得分:0)

嗯,你可以轻松地将它们结合起来:

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}";

当然还有其他方法可以组合字符串,例如:

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

但是,如果你想第二部分包含在第一个字符串的状态中,它看起来会有点不同:

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()}";

现在它需要最后一行,将其从第一部分中删除并将其添加到最后一部分。格式化现在不是最佳的,但我怀疑这是否相关。

但是,如果你想这样做,那么,使用XmlDocuments,你可以使用它:

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();
}

这将获取字符串,从中创建文档,将第二个文档导入第一个文档,将其添加到第一个文档的最后一个节点(即 States 节点)并将其转换为一个字符串。

Here您可以阅读有关XmlDocuments的更多信息。

答案 3 :(得分:-1)

使用Replace解决此问题很容易,它比加载XML解析器要快得多。如果你有更复杂的XML字符串 - 也许你需要一个 - 如果你有更大和更多的字符串,那么最好使用StringBuilder.Append()而不是与+结合:

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包含结果。