xpath读取edmx文件的EntityTypes

时间:2017-06-18 16:17:41

标签: c# xml linq-to-xml edmx

我需要在属性节点的edmx文件中将StoreGeneratedPattern更新为“None”,基于某些类似于“属性”的标题包含“Code”值,XPath将选择哪些属性元素?

<?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" />

1 个答案:

答案 0 :(得分:0)

试试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");
        }
    }
}