C# Pass multiple parameters into XML using Queue - c#

I have a webservice that obtains data from Oracle CRM OnDemand. I want to create another WS that takes the fields for a given customer record and passes those fields as parameters into an XML writer which then creates the XML to be passed to another webservice (being developed by another team).
I can create the XML fine and pass the parameters. However, I am only testing 3-4 fields and passing them. I will need way more than 4 parameters to be passed, and I know this is pretty poor programming to create a function that takes 3+ parameters. (EDIT) I have created a queue that takes in the parameters from the page, and I pass just the queue. I want to dequeue the data field followed by a string of what I want to pass into the XML page.
This is what I have right now.
...for (int intIndx = 0; intIndx < intCount; intIndx++)
{
//Create queue to store all the data obtained from a given Account
Queue<string> q = new Queue<string>();
//Easier to reference
var acctName= objAcctQryOut.ListOfAccount[intIndx].AccountName;
var acctWebsite = objAcctQryOut.ListOfAccount[intIndx].WebSite;
q.Enqueue(acctName);
addyInsert.CountryCode = objAcctQryOut.ListOfAccount[intIndx].PrimaryBillToCountry;
q.Enqueue(addyInsert.CountryCode);
addyInsert.Line1 = objAcctQryOut.ListOfAccount[intIndx].PrimaryBillToStreetAddress;
q.Enqueue(addyInsert.Line1);
addyInsert.Line2 = accountInfo.PrimaryBillToStreetAddress2;
q.Enqueue(addyInsert.Line2);
addyInsert.Line3 = accountInfo.PrimaryBillToStreetAddress3;
q.Enqueue(addyInsert.Line3);
addyInsert.City = objAcctQryOut.ListOfAccount[intIndx].PrimaryBillToCity;
q.Enqueue(addyInsert.City);
addyInsert.CountyCodeVertex = accountInfo.PrimaryBillToCounty;
q.Enqueue(addyInsert.CountyCodeVertex);
addyInsert.StateProvinceCodeVertex = accountInfo.PrimaryBillToState;
q.Enqueue(addyInsert.StateProvinceCodeVertex);even know?
custResponse.CustomerNumber = accountInfo.FuriganaName;
q.Enqueue(custResponse.CustomerNumber);
custInput.BillingCurrency = accountInfo.CurrencyCode;
q.Enqueue(custInput.BillingCurrency);
custInput.Email = accountInfo.CustomText1;
q.Enqueue(custInput.Email);
custInput.TravelPortCustomerTypeCode = accountInfo.AccountType;
q.Enqueue(custInput.TravelPortCustomerTypeCode);
var count = q.Count;
var xml = new XML();
xml.sendAccountInfoXML(q);
//xml.sendAccountInfoXML(acctName, addyInsert.CountryCode, addyInsert.Line1, addyInsert.Line2, addyInsert.Line3, acctWebsite);
}
private void createXML(Queue<string> q)
{
string saveLoc = #"C:\Users\XML Test\test{0}.xml";
XmlTextWriter writer = new XmlTextWriter(saveLoc, System.Text.Encoding.UTF8);
XmlDocument doc = new XmlDocument();
writer.WriteStartDocument(true);
writer.Formatting = Formatting.Indented;
writer.Indentation = 2;
writer.WriteStartElement("HEADER");
createNode(q, writer);
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
doc.Save(writer);
}
private void createNode(Queue<string> q, XmlWriter writer)
{
Queue<string> x = new Queue<string>();
x.Enqueue("ACCOUNTNAME");
x.Enqueue("BILLINGCOUNTRY");
x.Enqueue("BILLINGLINE1");
x.Enqueue("BILLINGLINE2");
x.Enqueue("BILLINGLINE3");
x.Enqueue("BILLINGCITY");
x.Enqueue("BILLINGCOUNTY");
x.Enqueue("BILLINGSTATE");
x.Enqueue("CUSTOMERNUMBER");
x.Enqueue("BILLINGCURRENCY");
x.Enqueue("CUSTOMEREMAIL");
x.Enqueue("CUSTOMERTYPE");
//x.Enqueue("CUSTOMURL");
writer.WriteStartElement("ACCOUNTINFO");
for (int i = 0; i < x.Count; i++)
{
writer.WriteStartElement(x.Dequeue());
for (int j = 0; j < q.Count; j++)
{
writer.WriteString(q.Dequeue());
writer.WriteEndElement();
}
}
writer.WriteEndElement();
}
However, I feel that the nested loops are not properly functioning. Would you mind trying to guide me through it and point out where I'm messing up? Thank you.

Related

StreamWriter along with XmlSerializer in DotNet6 does not write WhiteSpaces?

Actually I tried this basic code in C# which serializes the DataSet into XML document.
static void Main(string[] args)
{
XmlSerializer ser = new XmlSerializer(typeof(DataSet));
// Creates a DataSet; adds a table, column, and ten rows.
DataSet ds = new DataSet("myDataSet");
DataTable t = new DataTable("table1");
DataColumn c = new DataColumn("thing");
t.Columns.Add(c);
ds.Tables.Add(t);
DataRow r;
for (int i = 0; i < 10; i++)
{
r = t.NewRow();
r[0] = "Thing " + i;
t.Rows.Add(r);
}
StreamWriter writer = new StreamWriter("DemoNet5.xml");
ser.Serialize(writer, ds);
writer.Close();
}
And this creates the "DemoNet5.xml" file with proper WhiteSpaces and Indentation in .NET 5. But When I try to run the same code in .NET 6
I am not getting the WhiteSapces and Indentation so that the contents of the xml are written in single line.
Later on when I try to read that .xml document I face issues.
Is there someone who can help me with this ? I want my xml contents to be in proper format that is with proper indetation and WhiteSpaces.
Xml file created using .NET6
I have also attached the XML document created using DotNet6.

Save Formatted Data In A New NotePad File

I've the following data format in a NotePad file:
123456:
7891011:
So with the following code, I can replace colon from every line and get the below result:
var line = File.ReadAllLines("D:\\Sample.txt");
for (int i = 0; i < line.Length; i++)
{
var fields = line[i].Remove(30);
MessageBox.Show(fields.ToString());
TextWriter txt = new StreamWriter("D:\\Demo.txt");
txt.Write(line[i]);
txt.Close();
}
Output:
123456
7891011
But when I try to save the formatted data in a new NotePad file, it only saves one data. Is there anything I am doing wrong?
You always create a new StreamWriter within the line loop and overwrite the existing data. Try:
var line = File.ReadAllLines("D:\\Sample.txt");
using (TextWriter txt = new StreamWriter("D:\\Demo.txt"))
{
for (int i = 0; i < line.Length; i++)
{
var fields = line[i].Remove(30);
//MessageBox.Show(fields.ToString());
txt.Write(line[i]);
}
txt.Close();
}

write xml file c# asp.net, and control file size

I create an xml file and write some information in it.
When i do this operation, i follow these steps:
I get data from db and map it my object list;
In the foreach iteration statement write the xml file.
But my data size changable.
My problem is my xml file size must not exceed 200 mb. If my data exceeds 200 mb. I have to divide into two parts, if exceeds 400 mb divide into three parts.
Can I control the data size before filling the xml file?
For example if my list object data size exceeds 200 mb, I will divide my list object into two parts.
I don't understand you write xml method, but you can try something like this.
Main idea is count length of each record in list
[Serializable]
public class MyData
{
public int id { get; set; }
public string data { get; set; }
}
static void Main(string[] args)
{
string fileName = string.Format("xmlFile_{0}.xml", DateTime.Now.ToString("dd.mm.ss"));
int dataLength = 0;
int limit = 100500;
List<MyData> list = new List<MyData>();
for (int i = 0; i <= 1000; i++)
{
Random r = new Random();
string preparedData = Convert.ToBase64String(
System.Text.Encoding.UTF8.GetBytes(r.Next(int.MaxValue).ToString())
);
MyData mydata = new MyData { id = i, data = preparedData };
StringWriter stringWriter = new StringWriter();
XmlDocument xmlDoc = new XmlDocument();
XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);
XmlSerializer serializer = new XmlSerializer(typeof(MyData));
serializer.Serialize(xmlWriter, mydata);
string xmlResult = stringWriter.ToString();
dataLength += xmlResult.Length;
if (dataLength <= limit)
// insert you write method here
File.AppendAllText(fileName, xmlResult);
else
{
// create new file or something else you want
}
list.Add(mydata);
}
Console.WriteLine("Finished");
Console.ReadLine();
}

C# language compare

i wan to compare two xml file and find their differences and similarities,
private void checkLanguage(string file1, string file2)
{
XmlDocument xmldoc1 = new XmlDocument();
XmlDocument xmldoc2 = new XmlDocument();
XmlNodeList xmlnode1;
XmlNodeList xmlnode2;
int i = 0;
int j = 0;
string str = null;
FileStream fs1 = new FileStream(file1, FileMode.Open, FileAccess.Read);
xmldoc1.Load(fs1);
FileStream fs2 = new FileStream(file2, FileMode.Open, FileAccess.Read);
xmldoc2.Load(fs2);
xmlnode1 = xmldoc1.GetElementsByTagName("data");
xmlnode2 = xmldoc2.GetElementsByTagName("data");
for (i = 0; i <= xmlnode1.Count - 1; i++)
{
str = xmlnode1[i].Attributes["name"].Value;
for (j = 0; j <= xmlnode2.Count - 1; j++)
{
if (str == xmlnode2[j].Attributes["name"].Value)
{
lblResult.ForeColor = Color.Green;
lblResult.Text += Environment.NewLine + xmlnode1[i].Attributes["name"].Value;
}
else
{
label4.ForeColor = Color.Red;
label4.Text += Environment.NewLine + xmlnode1[i].Attributes["name"].Value;
}
}
}
}
my problem is the similar languange in both xml file list out in the difference field also..how to solve this..
can someone help me? thanks
Best way to do this is using linq to xml. Load both xml's and reachout to language value to compare. The same way for other tags.
Thanks
AHARI
If doing a general diff is adequate I wouldn't use an XML parser at all. Instead, I'd use Google's DiffMatchPatch library. Here is some sample code below;
var dmf = new diff_match_patch();
var diffs = dmf.diff_main(FileOneAsString, FileTwoAsString);
After that you have a list of all the diff's. These are categorized as EQUAL, ADD, or DELETE. They show what transformations have to happen in file2 to arrive at file1. To make it more human readable I'd recommend using the semantic clean up method. There's an int you can set that determines how major the clean up is (I don't remember the name but you can find it easily if you go this route).
dmf.diff_cleanupSemantic(diffs);
At that point you can see what's equal and what's not by looping over the diffs, and looking at the strings (the diff is an object with the type and the string).
The library can be downloaded here http://code.google.com/p/google-diff-match-patch/ and you can test out how much semantic cleanup you want to do using the diff demo.
If all you want is the differences in the two files this will be a lot simpler than using an xml parser.

Telerik RenderReport

I have some problems with Telerik reports.
Feels like i have missed something...
I wanna create a list of reports, and then write them to ONE file.
But when i write it out i only get one page.
The writer writer over page 1 all foreach, so it just write one page.
But i want several pages... in this case 10.
Have tried write with FileStream, File and more...
Does anyone have a good idea?
public void WriteToFile()
{
string path = #"C:\";
string test = "test";
var report = new Report2();
var procceser = new ReportProcessor();
var list = new List<RenderingResult>();
for (int i = 0; i < 10; i++)
{
var res = procceser.RenderReport("PDF", report, null);
list.Add(res);
}
string filePath = Path.Combine(path, test);
var Writer = new BinaryWriter(File.Create(filePath));
foreach (var renderingResult in list)
{
Writer.Write(renderingResult.DocumentBytes);
}
Writer.Flush();
Writer.Close();
}

Categories