C#用XML中的一个元素包装多个元素

时间:2015-06-29 06:48:50

标签: c# xml

如何在XML文件中的元素中包装多个元素?

我有代码创建一个像这样的XDocument:

 doc = new XDocument(
     new System.Xml.Linq.XDeclaration("1.0", "UTF-8", string.Empty),
     new XElement("JobSvc",
         new XAttribute("environment", this.environment),
         new XAttribute("dateTo", DateTime.Today.ToShortDateString()),
         new XAttribute("dateFrom", DateTime.Today.AddDays(-7).ToShortDateString())
     ));
     return doc;

然后我有代码向此文档添加元素,例如:

  public void Append_ProcessName_Server_Type(XDocument doc)
  {//i will replace confidential info
      var name_Type_Server = new XElement("Wrapper",
                                 new XAttribute("x","******"),
                                 new XAttribute("x","x"),
                                 new XAttribute("x","x"));
      doc.Root.Add(name_Type_Server);
  }

然后我在上面创建的元素下添加更多元素:

 public void AppendExtractedRowsAsElements(XDocument doc)
 {
     var newElement = new XElement("processId", this.id,
                          new XAttribute("duration", this.duration),
                          new XAttribute("eventTime", this.eventTime),
                          new XAttribute("source", this.source),
                          new XAttribute("runNumber", run));
     doc.Root.Add(newElement);
 }

这样可以正常工作并生成如下输出:

<?xml version="1.0" encoding="utf-8"?>
<JobSvc dateAndTimeExecuted="2015-06-29T14:38:48.588082+08:00" environment="" dateFrom="6/22/2015" dateTo="6/29/2015">
  <process server="xxx" type="x" name="x" />
  <processId duration="12182" eventTime="6/5/2015 6:23:02 AM" source="***" runNumber="1">xxxxxx</processId>
  <processId duration="6574" eventTime="6/12/2015 4:47:36 AM" source="***" runNumber="2">xxxxxx</processId>

如何使其输出如下:其中所有processid元素都由process元素包装?

<?xml version="1.0" encoding="utf-8"?>
    <JobSvc dateAndTimeExecuted="2015-06-29T14:38:48.588082+08:00" environment="" dateFrom="6/22/2015" dateTo="6/29/2015">
      <process server="xxx" type="x" name="x" >
      <processId duration="12182" eventTime="6/5/2015 6:23:02 AM" source="***" runNumber="1">xxxxxx</processId>
      <processId duration="6574" eventTime="6/12/2015 4:47:36 AM" source="***" runNumber="2">xxxxxx</processId>
</process>

3 个答案:

答案 0 :(得分:0)

您不应该使用doc.Root.Add(newElement);。这会将元素添加到根目录中。
请尝试这样做:

public void AppendExtractedRowsAsElements(XDocument doc, XElement parent)
{
   var newElement = new XElement("processId", this.id,
                    new XAttribute("duration", this.duration),
                    new XAttribute("eventTime", this.eventTime),
                    new XAttribute("source", this.source),
                    new XAttribute("runNumber", run));
   parent.Add(newElement);

}

如下所示:

AppendExtractedRowsAsElements(doc,name_Type_Server);

答案 1 :(得分:0)

只需将新创建的processId元素添加到包装元素而不是文档的根元素,例如:

public void AppendExtractedRowsAsElements(XDocument doc)
{
    ......
    var process = doc.Root.Element("process");
    process.Add(newElement);
}

答案 2 :(得分:0)

您可以使用XDocument.Parse(string)

简单地构建它
        static XDocument MakeDoc()
        {
            string input =  
                "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<JobSvc environment=\"" + this.environment + "\"\n" +
                " dateTo=\"" + DateTime.Today.ToShortDateString() + "\"\n" +
                " dateFrom=\"" + DateTime.Today.AddDays(-7).ToShortDateString() + "\"\n" +
                "/>\n";


            XDocument doc = XDocument.Parse(input);
            return doc;
        }​