I need to update StoreGeneratedPattern to "None" in edmx file programmaticaly for Property nodes, based on some criteara like Name attribute contains "Code" value, what will be XPath to select such Property elements?
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="3.0" xmlns:edmx="http://schemas.microsoft.com/ado/2009/11/edmx">
<!-- EF Runtime content -->
<edmx:Runtime>
<!-- SSDL content -->
<edmx:StorageModels>
<Schema Namespace="Model.Store" Provider="System.Data.CData.DynamicsCRM" ProviderManifestToken="DynamicsCRM" Alias="Self" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns:customannotation="http://schemas.microsoft.com/ado/2013/11/edm/customannotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm/ssdl">
<EntityType Name="Account">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="varchar" StoreGeneratedPattern="Identity" Nullable="false" />
<Property Name="AccountCategoryCode" Type="varchar" StoreGeneratedPattern="Computed" />
<Property Name="AccountClassificationCode" Type="varchar" StoreGeneratedPattern="Computed" />
<Property Name="AccountNumber" Type="varchar" />
Try xml linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XElement root = doc.Root;
XElement entityType = root.Descendants().Where(x => x.Name.LocalName == "EntityType").FirstOrDefault();
XNamespace ns = entityType.GetDefaultNamespace();
XElement toChange = entityType.Elements(ns + "Property").Where(x => ((string)x.Attribute("Name")).Contains("Code")).FirstOrDefault();
toChange.SetAttributeValue("StoreGeneratedPattern", "None");
}
}
}
Related
I'm currently importing webparts using a file upload control containing collection of webPartDescription (XML)
<wrapper>
<webParts>
<webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
<metaData>
<type src="~/Web/Controls/Test/TestHeaderList.ascx" />
<importErrorMessage>Cannot import this Web Part.</importErrorMessage>
</metaData>
<data>
<properties>
<property name="UserCancelled" type="bool">False</property>
<property name="AllowedRoles" type="string" null="true" />
<property name="SourceFileName" type="bool">False</property>
<property name="ViewFilterExpression" type="string" />
<property name="UserCreated" type="bool">False</property>
<property name="HideExportButtons" type="bool">False</property>
<property name="RecordCount" type="bool">False</property>
<property name="TransferStatus" type="bool">False</property>
<property name="OrdersHeaderID" type="bool">False</property>
<property name="TransmitRecordCount" type="bool">False</property>
<property name="DataSource" type="Lobas.Demo.Web.Controls.Test+Views, Test.Demo.Web, Version=2.2.0.0, Culture=neutral, PublicKeyToken=null">None</property>
<property name="DeltaCount" type="bool">False</property>
<property name="OrdersHeaderBatchNo" type="bool">False</property>
<property name="DateCancelled" type="bool">False</property>
<property name="ResponseDescription" type="bool">False</property>
<property name="RunDate" type="bool">False</property>
<property name="DateCreated" type="bool">False</property>
<property name="ResponseCode" type="bool">False</property>
</properties>
<genericWebPartProperties>
<property name="Width" type="unit" />
<property name="Description" type="string" />
<property name="AllowEdit" type="bool">True</property>
<property name="ChromeType" type="chrometype">Default</property>
<property name="TitleIconImageUrl" type="string" />
<property name="Hidden" type="bool">False</property>
<property name="TitleUrl" type="string" />
<property name="AllowClose" type="bool">True</property>
<property name="AllowMinimize" type="bool">True</property>
<property name="AllowConnect" type="bool">True</property>
<property name="Height" type="unit" />
<property name="HelpMode" type="helpmode">Navigate</property>
<property name="Title" type="string">Orders Header List</property>
<property name="CatalogIconImageUrl" type="string" />
<property name="Direction" type="direction">NotSet</property>
<property name="AllowZoneChange" type="bool">True</property>
<property name="ChromeState" type="chromestate">Normal</property>
<property name="HelpUrl" type="string" />
<property name="AllowHide" type="bool">True</property>
<property name="ExportMode" type="exportmode">NonSensitiveData</property>
</genericWebPartProperties>
</data>
</webPart>
</webParts>
</wrapper>
i want to filter and fetch the value of property element inside "genericWebPartProperties" with attribute name ="Title"
eg : Orders Header List (in above xml)
i'm new to linq and xml. i tried several solutions but received null
My code behind looks like :
//Uploded xml containing multible webparts wraped under <wrapepr> root tag.
XDocument UploadedXmlWebparts = XDocument.Load(e.UploadedFile.FileContent);
// fetching individual webpart xml and storing in a Xelement Collection
var WebpartCollection = UploadedXmlWebparts.Root.Elements();
List<ClsImportWp> WebPartDescriptionList = new List<ClsImportWp>();
foreach (XElement webPart in WebpartCollection)
{
var TitleName = // Get the title tag value here
}
Use xml linq with a dictionary :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication166
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XElement genericWebPartProperties = doc.Descendants().Where(x => x.Name.LocalName == "genericWebPartProperties").FirstOrDefault();
XNamespace ns = genericWebPartProperties.GetDefaultNamespace();
Dictionary<string,string> dict = genericWebPartProperties.Elements(ns + "property")
.GroupBy(x => (string)x.Attribute("name"), y => (string)y)
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
}
}
}
I have this error (translation from French Visual Studio error) when i try to Xpath a XML Data from a database :
System.ArgumentException: 'Noncompliant characters in the path.'
Error from Visual Studio (French)
This is my code :
public partial class WebForm1 : System.Web.UI.Page
{
SqlConnection connection = new SqlConnection(#"Data Source=SA2426\SQLEXPRESS;Initial Catalog=Proteor;Persist Security Info=True;User ID=sa;Password= root");
string query = "SELECT Content From Template WHERE Id = '0108f7ac-7853-4543-8ec4-a04cb755ed46' AND Deleted = '0'";
protected void Page_Load(object sender, EventArgs e)
{
try
{
SqlCommand cmd = new SqlCommand(query, connection);
connection.Open();
SqlDataReader sdr = cmd.ExecuteReader();
XmlDocument doc = new XmlDocument();
if(sdr.HasRows)
{
while(sdr.Read())
{
string content = sdr["Content"].ToString();
System.Diagnostics.Debug.WriteLine(System.Web.HttpUtility.HtmlDecode(content));
// Test Decode
string contenu = System.Web.HttpUtility.HtmlDecode(content);
doc.Load(System.Web.HttpUtility.HtmlDecode(content));
XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("msbld", "http://schemas.microsoft.com/developer/msbuild/2003");
XmlNode FormId;
FormId = doc.SelectSingleNode("descendant::Properties/Property[attribute::Name='FormId']", ns);
System.Diagnostics.Debug.WriteLine(FormId.OuterXml);
}
}
}
finally
{
connection.Close();
}
}
}
I have to decode my XML, because special chars are included in this data, as "<",">"...
i don't know how is it possible... i copy/paste the "Test Decode" in a simple xml document so as to check the xml code, and it's alright :
<Root>
<StorageObject Name="OI-GENOUILLERE ODRA-v2" Id="0108f7ac-7853-4543-8ec4-a04cb755ed46" Type="Adviser.Proteor.Library.TemplateImpl">
<Properties>
<Property Name="TimeCreated" Value="27/07/2017 11:14:18" /><Property Name="Enabled" Value="True" /><Property Name="TimePublished" Value="01/01/0001 00:00:00" /><Property Name="TimeLastModified" Value="10/11/2017 09:09:29" /><Property Name="Description" Value="" /><Property Name="ObjectContent" Value=""<Template>
<Component Type="Adviser.Proteor.Library.TemplateImpl">
<ExtendedProperty Text="OI" Key="Classification" />
<ExtendedProperty Text="35176af2-b664-42d6-a7ba-5d06602b0500" Key="ClassificationId" />
<ExtendedProperty Text="GAO" Key="Activity" />
<ExtendedProperty Text="58d942ea-8cf7-4089-879c-f8633925473b" Key="ActivityId" />
<ExtendedProperty Text="OI 36" Key="AnatomicalLevel" />
<ExtendedProperty Text="76418da5-6830-4a49-aed0-b472c11c67c6" Key="AnatomicalLevelId" />
<ExtendedProperty Text="ORTHESE INF." Key="Type" />
<ExtendedProperty Text="13b33a27-57ce-4009-8812-00f8578982fd" Key="TypeId" />
<Version Version.Major="0" Version.Minor="1" />
<Properties>
<Property Name="TimeCreated" Value="27/07/2017 11:14:18" />
<Property Name="Enabled" Value="True" />
<Property Name="TimePublished" Value="01/01/0001 00:00:00" />
<Property Name="TimeLastModified" Value="10/11/2017 09:09:29" />
<Property Name="Description" Value="" />
</Properties>
<Component Type="Adviser.Proteor.Library.StepImpl">
<Properties>
<Property Name="FormId" Value="d442dce3-fc1f-49da-92c5-b25d89d76881" />
<Property Name="ValidationMode" Value="None" />
</Properties>
<Component Type="Adviser.Proteor.Library.Impl.Action.Task.OnPageLoadTrigger">
<Properties>
<Property Name="PageNumber" Value="1" />
</Properties>
<Component Type="Adviser.Proteor.Library.Impl.Action.Task.SetControlValueTask">
<Properties>
<Property Name="Text" Value="{agence}" />
<Property Name="ObjectId" Value="dc20a4e2-46d6-47fe-8c75-bf4d7ff012ba" />
</Properties>
</Component>
It's not the full code, but you can see that the code is correctly written.
Need help !!!
EDIT : There is something i've just seen at the moment. There is a xml code encapsulate in a main xml code :
the Value of the ObjectContent is not "finished" and starts with an other tag template
Per the docs for XmlDocument.Load, this overload:
Loads the XML document from the specified URL.
What you're passing it isn't an URL, it's a string that contains the XML content. This is why the error message states that you have invalid characters in your path - your XML isn't a path at all.
You want to use the XmlDocument.LoadXml method.
I have been learning LINQ to XML however I ran into a senario where I'm a little stuck.
If I have the following XML:
<root>
<planes />
<trains />
<cars>
<car name="civic">
<property name="4doors" />
<property name="4tires" />
</car>
<car name="f150">
<property name="2doors" />
<property name="4tires" />
</car>
<car name="crv">
<property name="4doors" />
<property name="4tires" />
</car>
<car name="scooter">
<property name="2tires" />
</car>
<car name="escape">
<property name="4doors" />
<property name="4tires" />
</car>
</cars>
</root>
How can I return a list of cars who has 4doors?
So far I've tried the following attempts:
// This will return a list of nulls
var fourDoorCars = xDoc.Descendants("cars").Descendants("car").Descendants("property").Where(x => x.Attribute("name").Value.Contains("4doors")).Select(x => x.Element("car")).ToList();
// This will return a list of all the 4doors properties.
var fourDoorCars = xDoc.Descendants("cars").Descendants("car").Descendants("property").Where(x => x.Attribute("name").Value.Contains("4doors")).ToList();
You can do this:
var query=xDoc.Descendants("car")
.Where(x=> x.Elements("property")
.Any(y=>y.Attribute("name").Value.Contains("4doors")));
I have the following xml "example"
<methods>
<method name="listcust" >
<objects>
<object name="ExampleTest" type="" version="">
<properties>
<property name="FirstName" type="text">
<mappings>
<mapping type="property">
<property name="fn" />
</mapping>
</mappings>
</property>
<property name="LastName" type="text">
<mappings>
<mapping type="property">
<property name="ln" />
</mapping>
</mappings>
</property>
<property name="BirthDate" type="datetime">
<mappings>
<mapping type="property">
<property name="bd" />
</mapping>
</mappings>
</property>
</properties>
</object>
</objects>
</method>
</methods>
and Im trying to select the "name" atribute value of say
<property name="LastName" type="text">
when the "name" of the attribute within that element
is equal to "ln" <property name="ln" />
I tried the following
var query = from p in xdoc.Descendants("property")
where p.Descendants("property").Attributes("name").Contains(new XAttribute("name", "ln"))
select p.Attribute("name").Value;
var res = query.ToList<string>();
but I get zero results back.
If I break it down step by step
XDocument xdoc = XDocument.Parse(xml);
var props = xdoc.Descendants("property").Descendants("property");
var nameAtt = props.Attributes("name");
var contains = nameAtt.Contains(new XAttribute("name", "ln"));
Then I can see that nameAtt contains a list of the attributes including the <property name="ln" /> one but the contains bool is still false
If anyone can please point out the correct way of doing the select\where or perharps even a completely different more efficient way of retrieving the one attribute value where its child node attribute value equals the one passed in
Thanks
You just need to change the Where condition:
where p.Descendants("property").Any(x => (string)x.Attribute("name") == "In")
Any will check if there is any element that has name attribute equals to In
My head is fuzzled with this. I have an xml doc which has the layout for a grid stored in it. If you notice the columns are stored as "Items" in the XML. I am trying to retrieve each "Item" out of the XML using LINQ but no matter what I do I keep taking on straggler properties that I don't need. Any help would be much appreciated.
<?xml version="1.0" encoding="utf-16" ?>
<DatagridView>
<ViewType>DevExpress.XtraGrid.Views.Grid.GridView</ViewType>
<ViewLayout>
<property name="Columns" iskey="true" value="9">
<property name="Item1" isnull="true" iskey="true">
<property name="VisibleIndex">0</property>
<property name="Visible">true</property>
<property name="Width">1249</property>
<property name="SummaryItem" isnull="true" iskey="true">
<property name="SummaryType">Count</property>
<property name="DisplayFormat">{0}</property>
<property name="FieldName">Comments</property>
<property name="Tag" isnull="true" />
</property>
<property name="Name">colComments</property>
<property name="ColumnEditName" />
<property name="FieldName">Comments</property>
</property>
<property name="Item2" isnull="true" iskey="true">
<property name="VisibleIndex">1</property>
<property name="Visible">true</property>
<property name="Width">197</property>
<property name="Name">colEvent</property>
<property name="ColumnEditName" />
<property name="FieldName">Event</property>
</property>
......
EDIT:
To be clear, the XML can contain any number of columns represented like so:
<property name="Item2" isnull="true" iskey="true">
<property name="VisibleIndex">1</property>
<property name="Visible">true</property>
<property name="Width">197</property>
<property name="Name">colEvent</property>
<property name="ColumnEditName" />
<property name="FieldName">Event</property>
</property>
I would like to grab those chunks of data in that same order via XML.
Well, you could just get the property elements directly beneath "Columns":
// TODO: Work out what to do if there are zero or multiple such elements
var columns = xdoc.Descendants("property")
.Where(x => (string) x.Attribute("name") == "Columns")
.Single();
var items = columns.Elements("property");
foreach (var item in items)
{
Console.WriteLine("Item {0}", (string) item.Attribute("name"));
foreach (var property in items.Elements("property"))
{
Console.WriteLine(" {0} = {1}", (string) item.Attribute("name"),
(string) item);
}
}