I am a newbie in c#, i have to convert an excel file to an xml with simple nodes. I have done it so far with interop. I have written parts of code to open an xls , close an xls, get value of a cell by Get_range method, and release the object.
But, now i have to output an xml file, i have to goto a particular cell and print a node and a value of a cell. It can be a simple plain text also which i can output or we can use the linq Xelement and Xattributes as well. I think when using get_range:
xlWorkSheet.get_Range("B3", "B3").Value2
i need to print the same in excel.
I am not sure how to do this, please guide me. The resulting xml should look something like this:
<WpData>
<WpType>Design</WpType>
<ReviewType>half</ReviewType>
<References>[1] https:///SysService/SysService_AsrDet/trunk/_doc/20_Design
</References>
<Author>deh</Author>
<Reviewer>abc</Reviewer>
</WpData>
thanks in advance.
You searching for xml serialization. You should define class like this:
[Serializable]
public class WpData
{
public string WpType { get; set; }
public string ReviewType { get; set; }
public string References { get; set; }
public string Author { get; set; }
public string Reviewer { get; set; }
}
Note Serializable
attribute on class.
Then you should fill your object from excel data and use XmlSerializer
:
WpData xmlSerializibleObject = new WpData();
//....
//here you should fill it from excel based on your data
//And then you can just serialize it to string
string xmlString;
XmlSerializer xmlSerializer = new XmlSerializer(xmlSerializibleObject.GetType());
using(StringWriter textWriter = new StringWriter())
{
xmlSerializer.Serialize(textWriter, xmlSerializibleObject);
xmlString = textWriter.ToString();
}
xmlString
will have xml that you need is you fill your object correct from excel.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.