5

I have to make an xml like this and post to a url on fly

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

What needs to be done to embed the string marksxml into XDocument?

2 Answers 2

6

Just parse marksxml as an XElement and add that:

XDocument doc = new XDocument(
    new XElement("Student",
        new XElement("Name", "John"),
        new XElement("Age", "17"),
        XElement.Parse(marksxml)
    );
)
Sign up to request clarification or add additional context in comments.

Comments

4

1.First get rid of this tag

</Student>

in marksxml, because it will give you an exception when you parse.

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

2.Then you create an XElement out of your string:

XElement marks = XElement.Parse(marksxml);

3.Now you add your new XElement to the student doc:

doc.Root.Add(marks);

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.