C#主节点属性的selectNodes



我正在尝试获得此XML文件的主节点的逻辑名称属性的值:

<?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;

但是我没有节点。我已经尝试了没有名称空间,也没有节点,儿子继续前进。�可以请任何人帮助我吗?

预先感谢。

如果要获得值:target.xml尝试此代码

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

首先您的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;

相关内容

  • 没有找到相关文章

最新更新