How to add Inner Xelements of an Xelement to other XElement at some specific position in asp.net - xelement

This is Xelement("elementExamQuestion ") I have created.
XElement elementExamQuestion = new XElement("ExamQuestions",
new XAttribute("id", xmodel.examQuestionID),
new XAttribute("srno", xmodel.examqsrno),
new XAttribute("isnegative", masterquesResult.qisnegative == null ? "0" : masterquesResult.qisnegative.ToString()),
new XAttribute("quesstatus", "NV"),
new XElement("QuestionId", dr.questionid.ToString()),
new XElement("QuestionNo", ""),
new XElement("Question", dr.question.ToString().Trim()),
**<----At This position----->**
new XElement("TopicId",dr.topicid.ToString()),
new XElement("ExamId", xmodel.GI.CE_examid.ToString().Trim()),
new XElement("TimeTaken",""),
new XElement("UTCQuesTime","")
);
Now other Xelement("options")
XElement options = new XElement("options",
new XElement("OptionA",new XAttribute("value","a"),dr.a.Trim()),
new XElement("OptionB",new XAttribute("value","b"),dr.b.Trim()),
new XElement("OptionC",new XAttribute("value","c"),dr.c.Trim()),
new XElement("OptionD",new XAttribute("value","d"),dr.d.Trim()),
new XElement("OptionE",new XAttribute("value","e"),dr.e.Trim()),
new XElement("OptionF",new XAttribute("value","f"),dr.f.Trim()),
new XElement("OptionG",new XAttribute("value","g"),dr.g.Trim()),
new XElement("OptionH",new XAttribute("value","h"),dr.h.Trim())
);
I want to insert Xelements optionA, OptionB,Option C upto Option H in Xelement("elementExamQuestion") but after XElement("Question").
If anyone can help me it will be great. Thanks in advance!!!

After research I got exact solution
elementExamQuestion.Descendants("Question").ElementAt(0).AddAfterSelf(options.Elements());

Related

Extracting SPWeb.Groups.Xml in XElement

I need to get SPWeb.Groups.Xml in XElement to create XDocument.
SPSite site = new SPSite(url);
foreach (SPWeb web in site.AllWebs)
{
SPUserCollection spusers = site.RootWeb.SiteUsers;
XElement xeGroup = new XElement("Groups");
xeGroup = new XElement(web.Groups.Xml);}
currently I am getting error as "The '<' character, hexadecimal value 0x3C, cannot be included in a name.",
Please suggest workaround or correct way to retrieve the information.
Thanks for your help.
My solution, not very elegant...
SPSite site = new SPSite(url);
foreach (SPWeb web in site.AllWebs)
{
XElement xeGroup = new XElement("Groups");
xd.LoadXml(web.Groups.Xml);
xeGroup = XElement.Load(new XmlNodeReader(xd));

Unable to insert dynamic object to Elastic Search using NEST

I am creating a dynamic object. I assign the values via IDictionary. Add the collections of the IDictionary to the object. Then I add the dynamic object to Elastic Search using NEST code. It throws me stackoverflow exception."An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll"
Here is what I have tried.
var node = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(node,defaultIndex: "test-index");
var client = new ElasticClient(settings);
try
{
dynamic x = new ExpandoObject();
Dictionary<string, object> dic = new Dictionary<string, object>();
dic.Add("NewProp", "test1");
dic.Add("NewProp3", "test1");
x = dic;
var index3 = client.Index(x);
}
catch (Exception ex)
{
string j = ex.StackTrace;
}
I need to create an index in ElasticSearch, using a dynamic object, because I will be having an excel work book consisting of over 300 worksheet, and each and every sheet will be named as type, and the contents inside the worksheet will be the _source.
In the above example 'x' the dynamic object created is the name of the worksheet, and the values added into the dictionary are the rows and columns of excel sheet.
Where am I going wrong.
Regards,
Hema
I belive you can skip ExpandoObject and just index Dictionary<string, object>().
var dictionary = new Dictionary<string, object>();
dictionary.Add("NewProp", "test1");
dictionary.Add("NewProp3", "test1");
client.Index(dictionary);

Dynamic CRM : Need to update Connection entities via API

I have created connection via API through below code(Dynamics Crm: creating Connection entities via API
Entity connection = new Entity("connection");
connection["record1id"] = new EntityReference("contact", someContactId);
connection["record1objecttypecode"] = new OptionSetValue(2);
connection["record1roleid"] = new EntityReference("connectionrole", someConnectionRoleId);
connection["record2id"] = new EntityReference("incident", someCaseId);
connection["record2objecttypecode"] = new OptionSetValue(122);
connection["record2roleid"] = new EntityReference("connectionrole", someOtherConnectionRoleId);
var newId = service.Create(connection);
Question: I need to update connection role between two records.
If I understand the question correctly, you just need to proceed along the same road, using the Id you get from the Create to reference the record:
// Your code
Entity connection = new Entity("connection");
connection["record1id"] = new EntityReference("contact", someContactId);
connection["record1objecttypecode"] = new OptionSetValue(2);
connection["record1roleid"] = new EntityReference("connectionrole", someConnectionRoleId);
connection["record2id"] = new EntityReference("incident", someCaseId);
connection["record2objecttypecode"] = new OptionSetValue(122);
connection["record2roleid"] = new EntityReference("connectionrole", someOtherConnectionRoleId);
var newId = service.Create(connection);
// And then ...
connection = new Entity("connection"); // start from scratch
connection["connectionid"] = newId; // needed for the CRM to know what to update
connection["record1roleid"] = new EntityReference("connectionrole", yetAnotherConnectionRoleId);
connection["record2roleid"] = new EntityReference("connectionrole", yetAnotherAnotherConnectionRoleId);
service.Update(connection);

Regarding using cache in mvc3

I am using MVC3 and getting country table from database and storing in cache. First time it get the data from caching but when I refresh the page its give me only datatable schema only. It give me only fields countryid,countryname, countrycode. But it dont give me data.
DataTable dt_Country = (DataTable)HttpContext.Current.Cache["Countries"];
if (dt_Country == null)
{
DataTable dt_State1 = new DataTable();
SqlConnection con = new SqlConnection(Common.ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("ConstantsCountriesSelectAll", con);
SqlDataAdapter ada = new SqlDataAdapter(cmd);
ada.Fill(dt_State1);
HttpContext.Current.Cache["Countries"] = dt_State1;
dt_Country = (DataTable)HttpContext.Current.Cache["Countries"];
}
//dt_Country = (DataTable)HttpContext.Current.Cache["Countries"];
return dt_Country;

Retrieving related entities using RetrieveMultipleRequest

I have an entity called Invoice and an entity called InvoiceItem.
There is a one to many relationship called new_invoice_invoiceitem.
There is a LookupAttribute in InvoiceItem called new_parent_invoice_invoiceitem.
I am trying to retrieve the InvoiceItems that are related to the Invoice with a particular ID using the following code:
QueryExpression query = new QueryExpression();
query.EntityName = "new_invoiceitem";
query.ColumnSet = new AllColumns();
ConditionExpression condition = new ConditionExpression();
condition.AttributeName = "new_parent_invoice_invoiceitem";
condition.Values = new object [] { new Guid("fe1009cc-e034-49d5-bc59-ab4c3091a6f9") };
condition.Operator = ConditionOperator.Equal;
FilterExpression filter = new FilterExpression();
filter.AddCondition(condition);
query.Criteria = filter;
RetrieveMultipleRequest request = new RetrieveMultipleRequest();
request.Query = query;
RetrieveMultipleResponse response = (RetrieveMultipleResponse)crmService.Execute(request);
BusinessEntityCollection bec = response.BusinessEntityCollection;
The code runs without errors but the BusinessEntityCollection is always empty even though there are records in Dynamics.
Any idea what I'm doing wrong?
Thanks,
David
Try setting request.ReturnDynamicEntities = true

Resources