3

I want final xml as follows...

<Programs>
  <Program><id>4</id><isRead>true</isRead><isWrite>false</isWrite></Program>
  <Program><id>8</id><isRead>true</isRead><isWrite>true</isWrite></Program>
</programs>

now following is the code written

XmlDocument xmlDoc = new XmlDocument();
XmlNode rootNode = xmlDoc.CreateNode(XmlNodeType.Element,"programs",null);
xmlDoc.AppendChild(rootNode);


foreach (dynamic item in access)
{
  XmlNode myXmlNode = JsonConvert.DeserializeXmlNode(item.ToString(), "program");
  rootNode.AppendChild(myXmlNode); //error
}

where in myXmlNode.InnerXml, I am already getting following

<Program><id>4</id><isRead>true</isRead><isWrite>false</isWrite></Program>

And so, running the loop for all children to add in which is parent. But I am getting error for line //error as marked above. Error is:

The specified node cannot be inserted as the valid child of this node, because the specified node is the wrong type.

4
  • for the pretty printing part (multi-line and indentation) see stackoverflow.com/a/1123731/1132334 Commented Jan 25, 2018 at 12:22
  • JsonConvert.DeserializeXmlNode is interesting choice. Have you been here? Commented Jan 25, 2018 at 12:22
  • 1
    want to pass this xml as parameter to sql SP Commented Jan 25, 2018 at 15:41
  • @ghetal if you're looking to deserialize you can do this using the inverse of what I suggested in my answer below Commented Jan 26, 2018 at 10:49

3 Answers 3

2

You are trying to insert different type of xml node. You could use ImportNode to apply it.

foreach (dynamic item in access)
{
    XmlNode myXmlNode = JsonConvert.DeserializeXmlNode(item.ToString(), "program");
    rootNode.AppendChild(rootNode.OwnerDocument.ImportNode(myXmlNode.FirstChild,true));
}
Sign up to request clarification or add additional context in comments.

3 Comments

still gives error: Cannot import nodes of type 'Document'.
You can't import a document type as node. I updated my answer.
thanks.. with minimal changes I could achieve what I wanted
2

I would recommend you create a model for what you are looking to create in XML, then serialize a list of the model to XML.

The reason being that you will end up with something far more maintainable.

Your model would be something like this:

public class Program
{
    public int Id { get; set; }
    public bool IsRead { get; set; }
    public bool IsWrite { get; set; }
}

And you can serialize it by following this article:

https://support.microsoft.com/en-gb/help/815813/how-to-serialize-an-object-to-xml-by-using-visual-c

Comments

0

I've used the XmlSerializer library myself a few times.

public class Program
{
    public int Id { get; set; }
    public bool IsRead { get; set; }
    public bool IsWrite { get; set; }
}

public class XmlTemplate
{
    public List<Program> Programs { get; set; }
}

class MainProgram
{
    static void Main(string[] args)
    {
        var xmlTemplate = new XmlTemplate();
        xmlTemplate.Programs = new List<Program>();
        xmlTemplate.Programs.Add(new Program() { Id = 123, IsRead = true, IsWrite = false });
        xmlTemplate.Programs.Add(new Program() { Id = 456, IsRead = false, IsWrite = true });

        var serializer = new XmlSerializer(typeof(XmlTemplate));
        var stream = new StringWriter();

        serializer.Serialize(stream, xmlTemplate);

        Console.WriteLine(stream.ToString());
        Console.ReadKey();

    }
}

This outputs the XML in the following format:

<XmlTemplate xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Programs>
    <Program>
      <Id>123</Id>
      <IsRead>true</IsRead>
      <IsWrite>false</IsWrite>
    </Program>
    <Program>
      <Id>456</Id>
      <IsRead>false</IsRead>
      <IsWrite>true</IsWrite>
    </Program>
  </Programs>
</XmlTemplate>

2 Comments

have reached nearer to solution, so want to see if this way can it be achieved or not
It can. By using XmlSerializer and a template class, you can both serialize from object to XML and deserialize from XML to object.

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.