Rashmi kaneri

Rashmi kaneri

  • NA
  • 6
  • 5.7k

annot implicitly convert type 'System.Xml.XmlNode'

Aug 17 2016 2:51 AM
Below is my C# code to create xml file
using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.Linq;
using System.IO;
//using MSXML2;
namespace createxml
{
public class Sample
{
public static void Main()
{
// var objNode;
// var ObjHighLevel;
// var objGrandChildNode;
String strHighLevelInfoPath, strText, strNodes;
int IntCount;
string[] strNodesScplit,strSplit;
char[] delimiterChars = { ' ', ',', '.', ':', '\t' };
char[] stringdelimeter={'$','%'};
XmlDocument objDOM=new XmlDocument();
var strPath=@"c:\Temp\xml_file.xml";
var objNode = objDOM.CreateNode("Element", "Root", "");
objNode = objDOM.CreateProcessingInstruction("xml-stylesheet", "type='text/xsl' href='XmlReference/LogReport.xsl'");
objDOM.InsertBefore(objNode, objDOM.FirstChild);
objDOM.AppendChild(objNode);
strHighLevelInfoPath = @"C:\Inputs\HighLevelInfo.txt";
strText = File.ReadAllText(strHighLevelInfoPath);
strNodes = "MachineName,UserName,Application,TestingType,OperatingSystem,BuildVersion";
strNodesScplit = strNodes.Split(delimiterChars);
strSplit = strText.Split(stringdelimeter);
var ObjHighLevel = objDOM.CreateNode("Element","HighLevelInfo","");
var objGrandChildNode = objDOM.CreateNode("Element", "strNodesScplit", "");
objGrandChildNode.InnerText= System.Net.Dns.GetHostName();
ObjHighLevel.AppendChild (objGrandChildNode);
for (IntCount = 1; (IntCount <= strSplit.GetUpperBound(0)); IntCount++)
{
objGrandChildNode = objDOM.CreateNode("Element", "MachineName", "");
objGrandChildNode.InnerText = strSplit.GetValue(IntCount);
ObjHighLevel.AppendChild(objGrandChildNode);
}
objGrandChildNode = objDOM.CreateNode("Element", "ExecuteDateTime", "");
objGrandChildNode.InnerText = DateTime.Now.ToString();
ObjHighLevel.AppendChild(objGrandChildNode);
objNode.AppendChild(ObjHighLevel);
objDOM.AppendChild(objNode);
objNode = null;
objDOM.Save(strPath);
objDOM = null;
}
}
}
I am getting 2 errors
Error 1 Cannot implicitly convert type 'System.Xml.XmlNode' to 'System.Xml.XmlProcessingInstruction'. An explicit conversion exists (are you missing a cast?) C:\Users\Administrator\Documents\Visual Studio 2012\Projects\Rashmi1\Rashmi1\UnitTest5.cs 32 23 Rashmi1
Error 2 Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?) C:\Users\Administrator\Documents\Visual Studio 2012\Projects\Rashmi1\Rashmi1\UnitTest5.cs 54 43 Rashmi1
 
Please help 
 

Answers (1)