Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用c在属性级别读取具有名称空间的XML#_C#_Xml_Xmlreader_Readxml - Fatal编程技术网

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

C# 使用c在属性级别读取具有名称空间的XML#,c#,xml,xmlreader,readxml,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=

我有一个如下所示的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>
请建议

大家好,为了更好地理解我的问题,我正在有目的地更新这个问题

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

我将为这种方法编写一份XML文档。一个原因是,您可以简单地选择要使用的所有标记(在您的示例中是
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
}

我希望我能解决您的问题

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

前-

我从这篇文章中找到了这个答案

我想把这个XML作为类键的列表

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

通过下面的代码,您可以从xml中获取元素列表
add
inside
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; }
    }
输出:


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

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
公共类appSettings
{
[XmlElement(“添加”)]
公共列表附加项{get;set;}
}
公共类项目
{
[XmlAttribute(“键”)]
公共字符串密钥{get;set;}
[XmlAttribute(“值”)]
公共字符串值{get;set;}
[XmlAttribute(命名空间=”http://schemas.microsoft.com/XML-Document-Transform")]
公共字符串转换{get;set;}
[XmlAttribute(命名空间=”http://schemas.microsoft.com/XML-Document-Transform")]
公共字符串定位器{get;set;}
}
XmlSerializer ser=新的XmlSerializer(typeof(appSettings));
var settings=(appSettings)ser.Deserialize(File.Open(“test.xml”,FileMode.Open));

settings.AddItems//你能解释一下你想解决什么问题吗?为什么要使用自定义类而不是
ConfigurationManager
类从配置文件中提取信息?您好,Chandramouli,您能告诉我们有关您的问题的更多详细信息吗?嗨,伙计们,这不是实际的web.config文件。这是我们将在受保护服务器中维护的转换配置(不会在版本控制工具中提交)。我想为部署团队成员提供一个UI来更新这些文件。所以我们需要将其作为列表对象。介意共享不起作用的序列化代码吗?这里的问题是如何获取具有名称空间的属性值(如xdt:Transform和xdt:Locator)。我不确定您要的是什么,您能告诉我吗?在您的逻辑中,如果我想获取属性xdt:Transform和xdt:Locator的值,系统将通过一个名为“对象引用未设置为对象实例”的错误@Chandramouli复制您的XML并在代码中使用它。对于attributes标记,我使用了
xdt:Transform
,并将其作为第一行
Replace
的返回值,非常感谢。它工作得很好,实际上我不理解您使用“XDocument”对象提供的解决方案。但是在使用“XmlDocument”对象时,它工作得很好。标记为答案。@Chandramouli,不知怎么晚了才发布我的答案,但可能对你有帮助:)
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; }
    }
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