将XML拆分为字符串

时间:2015-11-03 13:19:33

标签: c# xml string

我希望将XML文档拆分为字符串列表,但我完全不知所措。

<RISKCALC-OUT>
  <AUTHENTICATION>
    <USERID></USERID>
  </AUTHENTICATION>
  <OPERATION-LIST>
    <OPERATION MODEL="PRV" SUBMODEL="GBR40EDF" VERSION="4.0" FUNCTION="EDFCLC" TARGET="New Platform">
      .......
    </OPERATION>
    <OPERATION MODEL="PRV" SUBMODEL="GBR40EDF" VERSION="4.0" FUNCTION="EDFCLC" TARGET="New Platform">
      .....
    </OPERATION>
  </OPERATION-LIST>
</RISKCALC-OUT>

我试图将此文件拆分为字符串,以便第一个操作节点下面的所有内容都在一个字符串中,然后第二个操作节点下面的所有内容都在第二个字符串中。

我一直在玩XmlNodeLists,但我还没到任何地方。我不需要一个完整的答案,只是如何将它们分开准备放入单独的字符串。

感谢您的帮助。

2 个答案:

答案 0 :(得分:3)

我会将LinQ用于Xml 维护更容易。

XElement xmla = XElement.Load("input.xml");
var results = from operationList in xmla.Descendants("OPERATION")
                select operationList.Value;
foreach (var result in results)
{
    Console.WriteLine(result);
}

编辑以充分利用两个世界
(实际上这里有三个世界:对象/字符串和XML)

保存数据的类:

class Operation
{
    public String Model { get; set; }
    public String SubModel { get; set; }
    // and so on ...

    public String TagContent { get; set; }
    public String FullTag{ get; set; }
}

制作linq查询:

XElement xmla = XElement.Load("input.xml");
var operations = from operation in xmla.Descendants("OPERATION")
                    select new Operation
                    {
                        Model = operation.Attribute("MODEL").Value,
                        SubModel = operation.Attribute("SUBMODEL").Value,
                        TagContent = operation.Value,
                        FullTag = operation.ToString(),

                    };
foreach (var operation in operations)
{
    Console.WriteLine(operation.Model + " - " + operation.SubModel+ " - " + operation.TagContent " - " + operation.FullTag);
}

此致

答案 1 :(得分:2)

您可以像这样使用LINQ to XML:

static void Main(string[] args)
{
    string path = "XMLFile1.xml";
    XDocument xdoc = XDocument.Load(path);
    var ops = xdoc.Descendants("OPERATION")
        .Elements()
        .Select(n => n.ToString())
        .ToList<string>();

    ops.ForEach(s => Console.WriteLine(s));
}

我使用了这个示例XML(为测试目的添加了随机内容):

<RISKCALC-OUT>
  <AUTHENTICATION>
    <USERID></USERID>
  </AUTHENTICATION>
  <OPERATION-LIST>
    <OPERATION MODEL="PRV" SUBMODEL="GBR40EDF" VERSION="4.0" FUNCTION="EDFCLC" TARGET="New Platform">
      <SUBNODES ID="1">
        <ANOTHER ID="56"></ANOTHER>
      </SUBNODES>
    </OPERATION>
    <OPERATION MODEL="PRV" SUBMODEL="GBR40EDF" VERSION="4.0" FUNCTION="EDFCLC" TARGET="New Platform">
      <SUBNODES ID="1">
        <ANOTHER ID="65"></ANOTHER>
      </SUBNODES>
    </OPERATION>
  </OPERATION-LIST>
</RISKCALC-OUT>

并使用上面的代码获得此输出:

<SUBNODES ID="1">
  <ANOTHER ID="56"></ANOTHER>
</SUBNODES>
<SUBNODES ID="1">
  <ANOTHER ID="65"></ANOTHER>
</SUBNODES>

要保留OPERATION节点,您可以删除Elements(),如下所示:

var ops = xdoc.Descendants("OPERATION")
            .Select(n => n.ToString())
            .ToList<string>();

会产生

<OPERATION MODEL="PRV" SUBMODEL="GBR40EDF" VERSION="4.0" FUNCTION="EDFCLC" TARGE
T="New Platform">
  <SUBNODES ID="1">
    <ANOTHER ID="56"></ANOTHER>
  </SUBNODES>
</OPERATION>
<OPERATION MODEL="PRV" SUBMODEL="GBR40EDF" VERSION="4.0" FUNCTION="EDFCLC" TARGE
T="New Platform">
  <SUBNODES ID="1">
    <ANOTHER ID="65"></ANOTHER>
  </SUBNODES>
</OPERATION>