Here is my code for creating an XML file
public void CreateXml(string[] names, string[] values, string type)
{
XElement xml = new XElement("Transaction",
new XElement("TransactionType", type));
foreach (var i in names)
{
foreach (var o in values)
{
xml.Add(new XElement(i.Replace(" ", string.Empty), o));
}
}
xml.Save("C:\\Users\\PHWS13\\Desktop\\sample.xml");
}
And my output looks like this
<?xml version="1.0" encoding="UTF-8"?>
<Transaction>
<TransactionType>Void</TransactionType>
<Zip>1</Zip>
<Zip>2</Zip>
<PNRef>1</PNRef>
<PNRef>2</PNRef>
</Transaction>
But it is not right, I am expecting more like this
<?xml version="1.0" encoding="UTF-8"?>
<Transaction>
<TransactionType>Void</TransactionType>
<Zip>1</Zip>
<PNRef>2</PNRef>
</Transaction>
As I have noticed the values are correct but it i just duplicates, How can I fix this?
string[] textBoxNamesArray = flowLayoutPanelText.Controls.OfType<TextBox>()
.Select(r => r.Name)
.ToArray();
string[] textBoxTextsArray = flowLayoutPanelText.Controls.OfType<TextBox>()
.Select(r => r.Text)
.ToArray();
You shouldn’t use two nested loops then: two nested loops create a Cartesian product (i.e. every name is paired up with every value).
If you use a single loop that goes through names and values at the same time, you’d get your expected output.
If you are using .NET 4, you could use
Zipmethod, like this: