主节点属性的C#SelectNodes

时间:2018-12-18 15:30:19

标签: c# xml xml-namespaces xmlnodelist

我正在尝试获取此xml文件主节点的logicalName属性的值:

<?xml version="1.0" encoding="ISO-8859-1"?>
<ticketlayout xmlns="http://www.example.com/ticketlayout" logicalName="target.xml" deviceCode="1" measurement="mm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com/ticketlayout">
    <fontdefinition id="BarCode">
         <fontname>Code128bWin</fontname>
         <size measure="pt">16</size>
    </fontdefinition>
</ticketlayout>

我尝试通过以下方式添加名称空间“ xsi”,“ http://www.w3.org/2001/XMLSchema-instance”:

XmlDocument fLayout = new XmlDocument();
fLayout.Load("myFile.xml");
XmlNamespaceManager nsmRequestLayout = new XmlNamespaceManager(fLayout.NameTable);
nsmRequestLayout.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
string sValue = fLayout.SelectNodes("//ticketlayout", nsmRequestLayout)[0].Attributes["name"].Value;

但是我没有节点。我试过没有命名空间,再也没有节点,然后继续。 ¿可以请任何人帮我吗?

谢谢。

2 个答案:

答案 0 :(得分:1)

如果要获取值:target.xml 试试这个代码

 XmlDocument fLayout = new XmlDocument();
        fLayout.Load("myFile.xml"); // your XML file
        var attrib = fLayout["ticketlayout"].Attributes["logicalName"].Value;

答案 1 :(得分:1)

首先,您的XML无效。 我进行了修改,以实现所需的目的。 XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<ticketlayout xmlns="http://www.example.com/ticketlayout" logicalName="target.xml" deviceCode="1" measurement="mm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com/ticketlayout">
    <fontdefinition id="BarCode">
         <fontname>Code128bWin</fontname>
         <size measure="pt">16</size>
    </fontdefinition>
</ticketlayout>

我不确定为什么没有模型结构来反序列化xml,然后访问所需的任何属性/属性。

示例: 班级:

 [XmlRoot(ElementName = "size", Namespace = "http://www.example.com/ticketlayout")]
    public class Size
    {
        [XmlAttribute(AttributeName = "measure")]
        public string Measure { get; set; }
        [XmlText]
        public string Text { get; set; }
    }

    [XmlRoot(ElementName = "fontdefinition", Namespace = "http://www.example.com/ticketlayout")]
    public class Fontdefinition
    {
        [XmlElement(ElementName = "fontname", Namespace = "http://www.example.com/ticketlayout")]
        public string Fontname { get; set; }
        [XmlElement(ElementName = "size", Namespace = "http://www.example.com/ticketlayout")]
        public Size Size { get; set; }
        [XmlAttribute(AttributeName = "id")]
        public string Id { get; set; }
    }

    [XmlRoot(ElementName = "ticketlayout", Namespace = "http://www.example.com/ticketlayout")]
    public class Ticketlayout
    {
        [XmlElement(ElementName = "fontdefinition", Namespace = "http://www.example.com/ticketlayout")]
        public Fontdefinition Fontdefinition { get; set; }
        [XmlAttribute(AttributeName = "xmlns")]
        public string Xmlns { get; set; }
        [XmlAttribute(AttributeName = "logicalName")]
        public string LogicalName { get; set; }
        [XmlAttribute(AttributeName = "deviceCode")]
        public string DeviceCode { get; set; }
        [XmlAttribute(AttributeName = "measurement")]
        public string Measurement { get; set; }
        [XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Xsi { get; set; }
        [XmlAttribute(AttributeName = "schemaLocation", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
        public string SchemaLocation { get; set; }
    }

然后您可以使用序列化程序:

public class Serializer
    {
        public T Deserialize<T>(string input) where T : class
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));

            using (StringReader stringReader = new StringReader(input))
            {
                return (T)xmlSerializer.Deserialize(stringReader);
            }
        }

        public string Serialize<T>(T ObjectToSerialize)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(ObjectToSerialize.GetType());
            StringBuilder builder = new StringBuilder();
            using (StringWriterWithEncoding textWriter = new StringWriterWithEncoding(builder, Encoding.UTF8))
            {
                xmlSerializer.Serialize(textWriter, ObjectToSerialize);
                return textWriter.ToString();
            }
        }
    }
    public class StringWriterWithEncoding : StringWriter
    {
        Encoding encoding;

        public StringWriterWithEncoding(StringBuilder builder, Encoding encoding)
        : base(builder)
        {
            this.encoding = encoding;
        }

        public override Encoding Encoding
        {
            get { return encoding; }
        }
    } 

最后,通过执行以下操作,您可以访问所需的任何内容:

var serializer = new Serializer();

//I used a local file for testing, but it should be the same thing with your file
var xmlInputData = File.ReadAllText(@"MyXmlPath");

var output = serializer.Deserialize<Ticketlayout >(xmlInputData);
var logicalName = output.LogicalName;