score:0
I am posting the working version of my requirement :)
XDocument currentPermission = XDocument.Parse(xmlParentStr);
XDocument newPermission = XDocument.Load(xmlToAddStr);
int fyValue = 2012, countryId = 205, stateId = 0, cityId = 0;
//check whether user has access for this fiscal year or not
IEnumerable<XElement> fyList = from fyNode in currentPermission.Descendants("FiscalYear")
where (int)fyNode.Attribute("Name") == fyValue
select fyNode;
if (null != fyList && fyList.Count() > 0) //Fiscal year present, means user is trying to modify permissions for the given fiscal year
{
//Check whether user has access for this country or not
IEnumerable<XElement> countryList = from subNode in fyList.Descendants("Country")
where (int)subNode.Attribute("ID") == countryId
select subNode;
if (null != countryList && countryList.Count() > 0) //country present, means user is trying to modify permissions for a country
{
IEnumerable<XElement> stateList = from mbpNode in countryList.Descendants("State")
where (int)mbpNode.Attribute("ID") == stateId
select mbpNode;
if (stateId != 0 && null != stateList && stateList.Count() > 0)
{
IEnumerable<XElement> cityList = from mbpNode in stateList.Descendants("City")
where (int)mbpNode.Attribute("ID") == stateId
select mbpNode;
if (cityId != 0 && null != cityList && cityList.Count() > 0)
{
// User already have access, nothing to do
}
else
{
currentPermission.Elements("FiscalYear")
.Where(t => t.Attribute("Name").Value == fyValue.ToString())
.Elements("Country")
.Where(t => t.Attribute("ID").Value == countryId.ToString())
.Elements("State")
.Where(t => t.Attribute("ID").Value == stateId.ToString())
.Single()
.Add(newPermission.Descendants("City"));
}
}
else
{
currentPermission.Elements("FiscalYear")
.Where(t => t.Attribute("Name").Value == fyValue.ToString())
.Elements("Country")
.Where(t => t.Attribute("ID").Value == countryId.ToString())
.Single()
.Add(newPermission.Descendants("State"));
}
}
else //Country is not present means user is granted permissions for this country
{
currentPermission.Elements("FiscalYear")
.Where(t => t.Attribute("Name").Value == fyValue.ToString())
.Single()
.Add(newPermission.Descendants("Country"));
}
}
else //fiscal year is not present, it means user is trying to add permissions for a new fiscal year
{
//string newPermissionXML = CreatePermissionXML(newUserPermission);
currentPermission.Add(newPermission.Descendants("FiscalYear"));
}
score:1
// untested
var masterDoc = XDocument.Load(...);
var updateDoc = XDocument.Load(...);
foreach (var year in updateDoc.Root.Descendants("FiscalYear"))
{
var oldYear = masterDoc.Root.Descendants("FiscalYear")
.Where(y => y.Attributes["ID"].Value == year.Attributes["ID"].Value)
.FirstOrDefault() ;
if (oldYear == null)
{
masterDoc.Root.Add(new XElement(....));
}
else
{
// nested properties
}
}
score:2
If you simply want to append a new node, this is quite simple:
var xmlDocument = XDocument.Parse(xmlParentStr);
var xmlToAdd = XElement.Parse(xmlToAddStr);
if (null != xmlDocument.Element("Security"))
xmlDocument.Element("Security").AddFirst(xmlToAdd);
Where xmlParentStr is a string that includes the xml that you first present, with a 2011 and 2010 FiscalYear tag. xmlToAdd is the a string with the xml for the fiscal year of 2012. This will give you:
<Security>
<FiscalYear ID="2012">
<Country ID="13">
<State ID="20">
<City ID="11"></City>
<City ID="32"></City>
</State>
</Country>
</FiscalYear>
<FiscalYear ID="2011">
<Country ID="23">
<State ID="10">
<City ID="1"></City>
<City ID="3"></City>
</State>
</Country>
</FiscalYear>
<FiscalYear ID="2010">
<Country ID="13">
<State ID="20">
<City ID="11"></City>
<City ID="32"></City>
</State>
</Country>
</FiscalYear>
</Security>
There are various other ways to load the xml into an XDocument or an XElement, by the way. Such as XDocument.Load() and XElement.Load which can pull it in from a FileStream, a URI, or a Reader.
If you then wanted to change the country id for 2011, you could easily do it via:
var elementToChange=xmlDocument
.Descendants()
.Where(x => x.Name.LocalName=="FiscalYear" && x.Attribute("ID")!=null && x.Attribute("ID").Value=="2011");
foreach(var element in elementToChange) {
var changes = element.Descendants().Where(x => x.Name.LocalName == "Country" && x.Attribute("ID").Value == "23");
foreach(var change in changes) {
change.Attribute("ID").SetValue("1337");
}
}
Which would then yield:
<Security>
<FiscalYear ID="2012">
<Country ID="13">
<State ID="20">
<City ID="11"></City>
<City ID="32"></City>
</State>
</Country>
</FiscalYear>
<FiscalYear ID="2011">
<Country ID="1337">
<State ID="10">
<City ID="1"></City>
<City ID="3"></City>
</State>
</Country>
</FiscalYear>
<FiscalYear ID="2010">
<Country ID="13">
<State ID="20">
<City ID="11"></City>
<City ID="32"></City>
</State>
</Country>
</FiscalYear>
</Security>
I made the value you need change to "1337" because it's easier to see, but the principle is the same. You can continue to walk down the tree in this manner. (And the below code would change every entry that matches, if you decided to only change the first or know you would only ever have one, you could simplify the above code and get rid of the foreach loops by using .FirstOrDefault())
Source: stackoverflow.com
Related Query
- Efficient way to merge 2 xmls into a new XML in LINQ
- How to flatten a multi level XML into a single level XML using c# code LINQ
- What's the best way to copy and insert data from related tables into a new set of related tables with Linq
- Can't add a new record with an integer value into database by using linq from code C#
- Efficient way to merge table like datastructure using linq
- Is this the most efficient way of loading xml values into a structure?
- Most efficient way of loading data into LINQ object for search result type method
- How to get the name of elements/attributes in xml files and write these into another new xml file using LINQ
- Merge multiple xml files into one using linq to xml
- sort objects in XML and write back into new XML file using C#, LINQ
- How to load xml code block into existing xml file at a specific node using linq
- Using Linq to group a list of objects into a new grouped list of list of objects
- Most efficient way to update with LINQ to SQL
- Merge two List<object> into one List in Linq
- Merge multiple Lists into one List with LINQ
- Is there a way to inline external functions into an EF Linq query?
- how to add/insert conditional node into XML using linq to XML
- LINQ merge List<IEnumerable<T>> into one IEnumerable<T> by some rule
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- LINQ to XML - How to use XDocument the right way
- What is the fastest/most efficient way to read this XML to Dictionary (Linq or something else?)
- LINQ Source Code Available
- Is there a way to select two values into same IEnumerable using LINQ in C#?
- Efficient way to unindent lines of code stored in a string
- Better way to cleanly handle nested XML with LINQ
- How can I combine this code into one or two LINQ queries?
- Is there a LINQ way to turn 2 line items into N based on quantity?
- Using Linq to group a list of objects that contains primitives data into a new grouped list of objects
- Advice on the best way to structure/format a LINQ to XML query?
- Using LINQ to parse XML into Dictionary
More Query from same tag
- How to make the given method generic ? Is it a good idea to make it generic in terms of performance?
- How to select from multiple tables using LINQ using a union query to return only one column?
- Entity Framework and LINQ with views defining Nested tables
- C# List<T> get return a sorted list
- C# linq how to search data between two date
- LINQ: Query to get information from 2 tables
- Linq to NHibernate multiple OrderBy calls
- Join in get api request one to many
- Building a method using lambda expressions call
- Search on binary field with LINQ
- Get the row with the max(DateTime) LINQ
- EBNF to fluent interface
- How to group according to a field value
- How can I do an OrderBy with a dynamic string parameter order on a join query
- Expression Trees: Filtered count on navigation property
- Linq dynamic expression for filtering navigation properties and collections
- Convert string to LINQ query
- difference between two dictionarys using linq
- LINQ Query based on values in a list?
- how to make Value cannot be null, null
- A problems with display data from linq queries in grid
- EF deferred execution using SQLquery as input
- C#, How to pass property name from string parameter for Dynamic object
- LINQ- Combine Multiple List<T> and order by a value (.Net 3.5)
- Xml, Linq to Class
- Why does using Random in Sort causing [Unable to sort IComparer.Compare error]
- SQL INNER JOIN vs Where Exists Performance Consideration
- Error convert Datetime to nullable Datetime in Linq
- How to convert int array to List<KeyValuePair<int, string>>?
- C# Linq equivalent for SQL script not responding