2015-06-19 72 views
3

我不得不作出這樣的,後一個XML到URL飛添加XML內容到的XDocument

<Student> 
<Name>John</Name> 
<Age>17</Age> 
<Marks> 
    <Subject> 
     <Title>Maths</Title> 
     <Score>55</Score> 
    </Subject> 
    <Subject> 
     <Title>Science</Title> 
     <Score>50</Score> 
    </Subject> 
</Marks> 
</Student> 

string marksxml = "<Marks><Subject><Title>Maths</Title><Score>55</Score></Subject><Subject><Title>Science</Title><Score>50</Score></Subject></Marks>"; 
XDocument doc = new XDocument(new XElement("Student", 
new XElement("Name", "John"), 
new XElement("Age", "17"))); 

需要採取哪些措施來嵌入字符串marksxml到的XDocument?

回答

2

只是解析marksxmlXElement並添加:

XDocument doc = new XDocument(
    new XElement("Student", 
     new XElement("Name", "John"), 
     new XElement("Age", "17"), 
     XElement.Parse(marksxml) 
    ); 
) 
3

1.首先擺脫這個標籤

</Student> 
在marksxml

的,因爲當你分析它會給你一個例外。

string marksxml = "<Marks><Subject><Title>Maths</Title><Score>55</Score></Subject><Subject><Title>Science</Title><Score>50</Score></Subject></Marks>"; 

2.然後你創建的XElement出你的字符串:

XElement marks = XElement.Parse(marksxml); 

3.Now你添加新的XElement的學生DOC:

doc.Root.Add(marks);