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.

Faris Kapo
  • 346
  • 9
  • 30
ghetal
  • 403
  • 2
  • 11
  • 30

3 Answers3

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));
}
lucky
  • 12,734
  • 4
  • 24
  • 46
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

ianrathbone
  • 2,334
  • 2
  • 15
  • 16
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>
Oystein
  • 1,232
  • 11
  • 26
  • have reached nearer to solution, so want to see if this way can it be achieved or not – ghetal Jan 25 '18 at 15:43
  • It can. By using XmlSerializer and a template class, you can both serialize from object to XML and deserialize from XML to object. – Oystein Jan 25 '18 at 15:55