1

我有一个如下的 XML 文件

<?xml version="1.0"?>
<appSettings xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <add xdt:Transform="Replace" xdt:Locator="Match(key)" key="Key1" value="TransformValue1"/>
  <add xdt:Transform="Replace" xdt:Locator="Match(key)" key="Key2" value="TransformValue2"/>
  <add xdt:Transform="Replace" xdt:Locator="Match(key)" key="Key3" value="TransformValue3"/>
  <add xdt:Transform="Replace" xdt:Locator="Match(key)" key="Key4" value="TransformValue4"/>

  <add xdt:Transform="Insert" key="Key6" value="TransformValue6"/>
</appSettings>

我想将此 XML 作为类键列表。这里的Key类如下

[Serializable]
public class Key
{
    public string Key { get; set; }
    public string Value { get; set; }
    public string Transform { get; set; }
    public string Locator { get; set; }
}

请建议

大家好,为了更好地理解我的问题,我有目的地更新问题。

目的: 作为自动部署的一部分,我们还计划自动部署 web.config 文件。为了实现这个过程,我们使用了“Web config transform”的概念。为了实现这种“Web 配置转换”,我们将在集中式服务器中维护转换文件(用于所有实例和客户端),这些文件将用于转换。但是为了更新转换文件,我们为部署团队成员提供了 Ui。为此,我们需要读取带有名称空间的 XML 配置。

4

4 回答 4

2

对于这种方法,我会使用 XmlDocument。一个原因是,您可以简单地选择add要使用的所有标签(在您的情况下)。其次,使用循环,您可以通过调用foreach轻松获取所有值Attributes

XmlDocument xdoc = new XmlDocument();

xdoc.LoadXml("YourXmlString");

XmlNodeList xNodes = xdoc.GetElementsByTagName("add");


foreach(XmlNode item in xNodes)
{    
    key = item.Attributes["state"].Value;

    //and so on
}

我希望我能解决你的问题

于 2018-10-04T09:04:42.623 回答
1

如果您创建模型来保存数据,那么您可以使用 2 行代码轻松地从文件中反序列化对象:

public class appSettings
{
    [XmlElement("add")]
    public List<Item> AddItems { get; set; }
}

public class Item
{
    [XmlAttribute("key")]
    public string Key { get; set; }
    [XmlAttribute("value")]
    public string Value { get; set; }
    [XmlAttribute(Namespace="http://schemas.microsoft.com/XML-Document-Transform")]
    public string Transform { get; set; }
    [XmlAttribute(Namespace="http://schemas.microsoft.com/XML-Document-Transform")]
    public string Locator { get; set; }
}

XmlSerializer ser = new XmlSerializer(typeof(appSettings));
var settings = (appSettings)ser.Deserialize(File.Open("test.xml", FileMode.Open));
settings.AddItems; //<- there is your list
于 2018-10-04T10:41:20.247 回答
0

您是否尝试过在此使用类的XPathSelectElements方法,XElement我们可以提供 xpath 来获取值

前任-

doc.XPathSelectElements("//add[@xdt:Transform!=text() or not(@xdt:Transform)]", doc.Root.CreateNavigator());

从这里阅读的这篇文章中找到了这个答案

于 2018-10-04T09:29:53.807 回答
0

我想将此 XML 作为类键列表。

在这里,我为您的演示目的创建了一个控制台应用程序。

通过下面的代码,您可以从您的 xml将您的元素列表获取addappSettings您的类中。Key

class Program
    {
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(@"Your xml here");
            XNamespace ns = doc.Root.GetDefaultNamespace();
            XNamespace xdt = "http://schemas.microsoft.com/XML-Document-Transform";

            var result = doc.Descendants(ns + "appSettings")
                .Elements(ns + "add")
                         .Select(x => new Key
                         {
                             Key1 = x.Attribute(xdt + "Transform") != null ? x.Attribute(xdt + "Transform").Value : "",
                             Value = x.Attribute(xdt + "Locator") != null ? x.Attribute(xdt + "Locator").Value : "",
                             Transform = x.Attribute("key") != null ? x.Attribute("key").Value : "",
                             Locator = x.Attribute("value") != null ? x.Attribute("value").Value : "",
                         }).ToList();


            result.ForEach(x => Console.WriteLine($"Transform: {x.Transform}, \t Locator: {x.Locator}, \t Key: {x.Key1}, \t Value: {x.Value}"));

            Console.ReadLine();
        }
    }



[Serializable]
    public class Key
    {
        public string Key1 { get; set; }
        public string Value { get; set; }
        public string Transform { get; set; }
        public string Locator { get; set; }
    }

输出:

在此处输入图像描述

于 2018-10-04T10:07:48.297 回答