Skip to content

Instantly share code, notes, and snippets.

@rhyous
Last active March 30, 2016 18:20
Show Gist options
  • Save rhyous/5176671fe33113bbc9808eb6e76ab1e6 to your computer and use it in GitHub Desktop.
Save rhyous/5176671fe33113bbc9808eb6e76ab1e6 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization; // Had to add this
using Rhyous.EasyXml; // Added this from NuGet package
namespace XmlAddIncrementWithMaxId
{
[XmlRoot("tabWyt")]
public class TabWyt : List<Table1>
{
public int GetNextLp()
{
return Count > 0 ? this.Max(o => o.Lp) + 1 : 0;
}
new public void Add(Table1 t)
{
t.Lp = Math.Max(GetNextLp(), t.Lp);
base.Add(t);
}
}
public class Table1
{
[XmlElement("LP")]
public int Lp { get; set; }
[XmlElement("BLOCK")]
public string Block { get; set; }
[XmlElement("ARBPL")]
public string Arbpl { get; set; }
}
class Program
{
static void Main(string[] args)
{
var myTabWyt = Serializer.DeserializeFromXml<TabWyt>(ref Xml);
var addedTable1 = new Table1 { Block = "ABC", Arbpl = "XYZ01" };
myTabWyt.Add(addedTable1);
var newXmlString = new Xml(Serializer.SerializeToXml(myTabWyt));
Console.WriteLine(newXmlString.PrettyXml);
}
private static string Xml = @"<?xml version=""1.0"" standalone=""yes""?>
<tabWyt>
<Table1>
<LP>3818326</LP>
</Table1>
<Table1>
<LP>3818327</LP>
<BLOCK>CASH</BLOCK>
<ARBPL>JMSMF01</ARBPL>
</Table1>
<Table1>
<LP>3818328</LP>
<BLOCK>CACH</BLOCK>
<ARBPL>JMSMF01</ARBPL>
</Table1>
<Table1>
<LP>3818329</LP>
<BLOCK>CACH</BLOCK>
<ARBPL>JMSMF01</ARBPL>
</Table1>
<Table1>
<LP>3818330</LP>
<BLOCK>CACH</BLOCK>
<ARBPL>JMSMF01</ARBPL>
</Table1>
</tabWyt>";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment