使用C#在属性级别读取具有名称空间的XML

时间:2018-10-04 08:56:47

标签: c# xml xmlreader readxml

我有一个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类的列表获取。这里的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配置转换”的概念。 为了实现此“ Web配置转换”,我们将在集中式服务器中维护转换文件(用于所有实例和客户端),并将这些文件用于转换。 但是,为了更新转换文件,我们为部署团队成员提供了Ui。为此,我们需要读取带有名称空间的XML配置。

4 个答案:

答案 0 :(得分:1)

对于这种方法,我将使用XmlDocument。原因之一是,您可以简单地选择要使用的所有标签(在您的情况下为String query = "SELECT a.col1, b.col2 FROM tableA a INNER JOIN tableB b "; query += "ON a.key = b.key; INSERT INTO tableC (col1) VALUES ('hello')"; String pattern = "FROM\\s+(\\S+)|JOIN\\s+(\\S+)|INSERT\\s+INTO\\s+(\\S+)"; Pattern r = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE); Matcher m = r.matcher(query); while (m.find()) { String match = m.group(1) != null ? m.group(1) : (m.group(2) != null ? m.group(2) : m.group(3)); System.out.println(match); } tableA tableB tableC )。其次,通过add循环,您可以通过foreach调用轻松获得所有值

Attributes

希望我能解决您的问题

答案 1 :(得分:0)

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

ex-

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

我在这篇from here

的帖子中找到了这个答案

答案 2 :(得分:0)

  

我想将此XML作为Key类的列表获取。

在这里,我创建一个控制台应用程序用于演示。

通过以下代码,您可以从xml中将add中的元素appSettings列表添加到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; }
    }

输出:

enter image description here

答案 3 :(得分:0)

如果创建用于保存数据的模型,则可以使用两行代码轻松地从文件中反序列化对象:

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