I have a web service to an SSRS server from which I download and serialize reports to XML in C#, and then later save as .rdl files.
I have the possibility to get the all the Data Source's aswell, with
ReportingService2005 rs = new ReportingService2005();
DataSource dataSource = new DataSource();
DataSourceDefinition dataSourceDefinition = rs.GetDataSourceContents(path);
dataSource.Item = dataSourceDefinition;
I want to save the data source objects as .ds or .rds files but I don't know how. Is there a way I can do this using XML serializing or are there any easier methods?
I had the same issue, I used the XML serialize to serialize the DataSource object here is the code snipet:
DataSource ds = new DataSource();
DataSourceDefinition dsDefinition = rs.GetDataSourceContents(item.Path);
ds.Item = dsDefinition;
sOutFile = string.Format(#"{0}{1}.rds", sOutputDir, item.Name);
if (File.Exists(sOutFile))
File.Delete(sOutFile);
SerializeObject(sOutFile, ds); // serializes the dataSource object and save it
here is the SerializeObject method, it serializes and then saves the file in XML format:
private void SerializeObject(string filename, DataSource d)
{
try
{
XmlSerializer serializer =
new XmlSerializer(typeof(DataSource));
// Create an XmlTextWriter using a FileStream.
Stream fs = new FileStream(filename, FileMode.Create);
XmlWriter writer =
new XmlTextWriter(fs, Encoding.Unicode);
// Serialize using the XmlTextWriter.
serializer.Serialize(writer, d);
writer.Close();
}
catch (Exception e) { MessageBox.Show("inside serializeObject Method "+e.Message); }
}
I hope this helps if someone is stuck with this issue
As kyzen suggested in the comment, I used a combination of some custom code and this post from SO Listing all Data Sources and their Dependencies (reports, items, etc) in SQL Server 2008 R2 to get the result I wanted.
Related
I have a similar case - I receive a DataSet object with data tables willed with data (e. g. customers table ...) from an external module (done by other programmer).
I then save the data set object to an xml using the writeXml method - here is my code
public void Save(string myXmlFilePath)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = System.Text.Encoding.UTF8;
settings.Indent = true;
settings.NewLineHandling = NewLineHandling.None;
using (XmlWriter writer = XmlWriter.Create(myXmlFilePath, settings))
{
ExportObject.WriteXml(writer);
writer.Close();
}
}
I get the standard xml file output structure like this:
<NewDataSet>
<Customers>...</Customers>
<Customers>...</Customers>
<NewDataSet>`
However I want the structure like this
<Customers>
<Customer>...</Customer>
<Customer>...</Customer>
</Customers>`
How can I achieve this?
Specify the desired names to the dataset and the tables within it.
ExportObject.DataSetName = "Customers";
ExportObject.Tables[0].TableName = "Customer";
A work around is to let WriteXml write however it likes and then do an XSD transform to the format you prefer.
I created a C# Application to write data to xml file. It overrides all the new Data,
How to Avoid..?
Please help me.
This is my code
namespace BarcodeScaner
{
class WriteFile
{
DateTime dt=DateTime.Now;
public WriteFile()
{
}
public void createFile(string ptr,string value)
{
using (XmlWriter writer = XmlWriter.Create("Data.xml"))
{
writer.WriteStartDocument();
writer.WriteStartElement("Products");
writer.WriteStartElement("Details");
writer.WriteAttributeString("PTR", ptr);
writer.WriteAttributeString("Value", value);
writer.WriteAttributeString("DateTime", dt.ToString());
writer.WriteEndElement();
writer.WriteEndDocument();
}
}
}
}
You can use XmlDocument to add custom nodes to an existing Xml Document:
XmlDocument doc = new XmlDocument();
doc.Load("Data.xml");
XmlElement el = doc.CreateElement("child");
el.InnerText = "Example of data being appendeed";
doc.DocumentElement.AppendChild(el);
doc.Save("test.xml");
You need to read the existing file, add your data to it. And then write the combined results to the file.
Because of the format of XML files, you can't simply append new data to the file as you might with some file formats.
I'm assuming that what you're wanting to do is add new data to an existing XML file, correct?
You can't just append data to an XML file, and your code actually creates a new file each time it's run, overwriting the old one.
What you need to do is read the XML data into memory, add the new nodes, and write the whole file out again. If there is a lot of data then it may be more efficient to stream from one file to another and insert the new nodes as you go.
I have a c# web MVC application.
I wish to serialize my model object to an XML to be stored in a SQL database field of type XML?
I was able to serialize to a file using:
var writer = new System.Xml.Serialization.XmlSerializer(typeof(car));
var file = new System.IO.StreamWriter(#"C:\car.xml");
Writer.Serialize(file, car);
file.Close();
How can I modify this code to serialize to type XML of which I can then store into my SQL table and field XML type
You can use StringWriter with XmlWriter to produce the xml string then save to your database
string xmlResult = string.Empty;
var xmlSerializer = new XmlSerializer(typeof(car));
using (var stringWriter = new StringWriter())
{
using (var xmlWriter = XmlWriter.Create(stringWriter))
{
xmlSerializer.Serialize(xmlWriter, car);
}
xmlResult = stringWriter.ToString();
}
// save xmlResult to DB
I have a link like translate.google.com/translate_a/t?client=t&text=like&hl=en&sl=en&tl=bn&ie=UTF-8&oe=UTF-8&multires=1&otf=2&ssel=4&tsel=0&otf=1&ssel=4&tsel=0&sc=1.
Here in text=like it will change like text=book text=pen, which means it will be my input word, and I will loop it for 1000 times.
I'm making a dictionary. The above url outputs JSON data.
I want to loop through 1000 words and get their json output into one text file - how can I do that in c#?
see this sample maybe usfull
Person person = GetPerson();
using (FileStream fs = File.Open(#"c:\person.json", FileMode.CreateNew))
using (StreamWriter sw = new StreamWriter(fs))
using (JsonWriter jw = new JsonTextWriter(sw))
{
jw.Formatting = Formatting.Indented;
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(jw, person);
}
You will want to query for the JSON data, and parse it using c# JSON.
This question contains extensive information about how to parse JSON.
You can download the JSON by querying that page, using WebClient ToString. You can then pass this into the JSON parser.
Depending on what you want to do with the data (you're not very clear on this), you can then use the JSON object to manipulate it.
Alternatively, if you just want to download the data to a file, you can use WebClient DownloadFile.
I would like to perform object serialization to only one branch in an existing XML file. While reading by using:
RiskConfiguration AnObject;
XmlSerializer Xml_Serializer = new XmlSerializer(typeof(RiskConfiguration));
XmlTextReader XmlReader = new XmlTextReader(#"d:\Projects\RiskService\WCFRiskService\Web.config");
XmlReader.ReadToDescendant("RiskConfiguration");
try
{
AnObject = (RiskConfiguration)Xml_Serializer.Deserialize(XmlReader);
AnObject.Databases.Database[0].name = "NewName";
}
finally
{
XmlReader.Close();
}
It is possible, I do not know how to edit the object again performed it can save the file without erasing other existing elements in an XML file. Can anyone help me?
I found a way to display the desired item serialization. How do I go now instead of the paste to the original element in XML?
StringWriter wr = new StringWriter();
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaration = true;
settings.Encoding = System.Text.Encoding.Default;
using (XmlWriter writer = XmlWriter.Create(wr, settings))
{
XmlSerializerNamespaces emptyNamespace = new XmlSerializerNamespaces();
emptyNamespace.Add(String.Empty, String.Empty);
Xml_Serializer.Serialize(writer, AnObject, emptyNamespace);
MessageBox.Show(wr.ToString());
}
First of all, you should stop using new XmlTextReader(). That has been deprecated since .NET 2.0. Use XmlReader.Create() instead.
Second, XML is not a random-access medium. You can't move forward and backwards, writing into the middle of the file. It's a text-based file.
If you need to "modify" the file, then you'll need to write a new version of the file. You could read from the original file, up to the point where you need to deserialize, writing the nodes out to a new version of the file. You could then deserialize from the original file, modify the objects, and serialize out to the new version. You could then continue reading from the original and writing the nodes out to the new version.