XML : Create new element with new attributes to existing node from view (XML)

I have a XML document with these values:

  <exceptions>  <exception exceptionId ="AC00765F-3427-4AF8-8B95-0104C30846C1" regionId="1" region="Asia" insideRegion="true" articleType="magazine" including="true" exclusive="true"  minShipTotal="10" maxShipTotal="20" />  </exceptions>    

My class looks like this (my Model looks the same):

   public class ShipException  {      public Guid ExceptionId { get; set; }        public int RegionId { get; set; }        public string Region { get; set; }        public bool InsideRegion { get; set; }        public string ArticleType { get; set; }        public bool Including { get; set; }        public bool Exclusive { get; set; }        public decimal MinShipTotal { get; set; }        public decimal MaxShipTotal { get; set; }  }    

The method in my controller:

   [HttpPost]      public ActionResult ShipExceptionCreate (ShipExceptionModel model)      {          var xmlLocation = settingsService.GetSettingByKey<string>(SettingKey.XmlLocationUrl.ToString());          string xmlPath = xmlLocation;            if (xmlLocation.StartsWith("~") || xmlLocation.StartsWith("/"))          {              xmlPath = System.Web.HttpContext.Current.Server.MapPath(xmlLocation);          }            var shipException = new ShipException()          {              SKUPrefix = model.SKUPrefix,              Region = model.Region,              Exclusive = model.Exclusive,              InsideRegion = model.InsideRegion,              Including = model.Including,              MaxShipCost = model.MaxShipCost,              MinShipCost = model.MinShipCost,              ExceptionId = model.ExceptionId            };            settingsDataProvider.CreateNewShipException(xmlPath, shipException);            return new NullJsonResult();      }    

The method in my data provider, where I use an interface for my method so that I can use the method in my controller:

  public void CreateNewShipException(string dataSource, ShipException shipExceptions)      {          var settingsXmlDocument = GetSettingsXmlDocument(dataSource);            XmlNode node = settingsXmlDocument.DocumentElement.SelectSingleNode("exceptions");          XmlElement element = settingsXmlDocument.CreateElement("exception");            element.Attributes["SKUPrefix"].Value =  shipExceptions.SKUPrefix; //CRASH HERE. I want to add all the other attributes with values here. HELP!            settingsXmlDocument.Save(dataSource);      }    

In have a grid in my view where I submit all the values. I can see all the values being displayed from my existing XML document.

My goal is to make a CRUD for the XML document. But first, how do I add the new element with the new attributes to my existing node in my XML document?

My XML document should look like this after:

  <exceptions>  <exception exceptionId ="AC00765F-3427-4AF8-8B95-0104C30846C1" regionId="1" region="Asia" insideRegion="true" articleType="magazine" including="true" exclusive="true"  minShipTotal="10" maxShipTotal="20" />              //THE NEW ELEMENT -> <exception exceptionId ="NEW GUID here" regionId="1" region="Asia" insideRegion="true" articleType="novel" including="true" exclusive="true"   minShipTotal="5" maxShipTotal="10" /></exceptions>    

No comments:

Post a Comment