C #: Read XML file with NameSpace

4

I want to get the values of these nodes in a XML file and each of these values assign a variable to it.

I get the file this way:

CODE:

var miXML = File.ReadAllText("D:\20101284981-01-0002-0000000060.xml");

XML:

    <?xml version="1.0" encoding="ISO-8859-1" standalone="no" ?>
    <ar:ApplicationResponse xmlns="urn:oasis:names:specification:ubl:schema:xsd:ApplicationResponse-2"
    xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
    xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
    xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
    xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2">

    <cbc:UBLVersionID>2.0</cbc:UBLVersionID>
    <cbc:CustomizationID>1.0</cbc:CustomizationID>
    <cbc:ID>201200000230061</cbc:ID>
    <cbc:IssueDate>2012-06-12</cbc:IssueDate>
    <cbc:IssueTime>10:09:27</cbc:IssueTime>
    <cbc:ResponseDate>2012-06-12</cbc:ResponseDate>
    <cbc:ResponseTime>10:09:30</cbc:ResponseTime>
    <cbc:Note>4031 - Debe indicar el nombre comercial</cbc:Note>
    <cbc:Note>4001 - El numero de RUC del receptor no existe.</cbc:Note>

    <ar:ApplicationResponse>

I tried XDocument and then declare the XNamespace but when I do an xdoc.load ("C: // ...."); he falls and sends me an error ['ar' is an undeclared prefix. line 2, position 2.]

    
asked by Cold Head Skillet 28.12.2016 в 18:33
source

1 answer

1

Your XML has two errors:

  • The nameSpace AR
  • is not declared
  • The ar tag is not closing: ApplicationResponse
  • I show you how to fix it:

    static void Main(string[] args)
        {
            /*
            //XML corregido, agregar namespace ar, cerrar tag ar
            string xml = @"<?xml version='1.0' encoding='ISO-8859-1' standalone='no' ?>
                        <ar:ApplicationResponse xmlns='urn:oasis:names:specification:ubl:schema:xsd:ApplicationResponse-2'
                        xmlns:ar='http://www.w3.org/2000/09/xmldsig#'
                        xmlns:cac='urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2'
                        xmlns:cbc='urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2'
                        xmlns:ds='http://www.w3.org/2000/09/xmldsig#'
                        xmlns:ext='urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2'>
                        <cbc:UBLVersionID>2.0</cbc:UBLVersionID>
                        <cbc:CustomizationID>1.0</cbc:CustomizationID>
                        <cbc:ID>201200000230061</cbc:ID>
                        <cbc:IssueDate>2012-06-12</cbc:IssueDate>
                        <cbc:IssueTime>10:09:27</cbc:IssueTime>
                        <cbc:ResponseDate>2012-06-12</cbc:ResponseDate>
                        <cbc:ResponseTime>10:09:30</cbc:ResponseTime>
                        <cbc:Note>4031 - Debe indicar el nombre comercial</cbc:Note>
                        <cbc:Note>4001 - El numero de RUC del receptor no existe.</cbc:Note>
                        </ar:ApplicationResponse>";
                        */
    
            //Para arreglar tu XML
    
            string xmlMAL = @"<?xml version='1.0' encoding='ISO-8859-1' standalone='no' ?>
                        <ar:ApplicationResponse xmlns='urn:oasis:names:specification:ubl:schema:xsd:ApplicationResponse-2'
                        xmlns:cac='urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2'
                        xmlns:cbc='urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2'
                        xmlns:ds='http://www.w3.org/2000/09/xmldsig#'
                        xmlns:ext='urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2'>
                        <cbc:UBLVersionID>2.0</cbc:UBLVersionID>
                        <cbc:CustomizationID>1.0</cbc:CustomizationID>
                        <cbc:ID>201200000230061</cbc:ID>
                        <cbc:IssueDate>2012-06-12</cbc:IssueDate>
                        <cbc:IssueTime>10:09:27</cbc:IssueTime>
                        <cbc:ResponseDate>2012-06-12</cbc:ResponseDate>
                        <cbc:ResponseTime>10:09:30</cbc:ResponseTime>
                        <cbc:Note>4031 - Debe indicar el nombre comercial</cbc:Note>
                        <cbc:Note>4001 - El numero de RUC del receptor no existe.</cbc:Note>
                        <ar:ApplicationResponse>";
    
            string xml = xmlMAL;
            //Agrego el namespace AR
            xml = xml.Replace("<ar:ApplicationResponse ", "<ar:ApplicationResponse xmlns:ar='http://www.w3.org/2000/09/xmldsig#' ");
            //Cierro el tag del <ar:ApplicationResponse>
            xml = xml.Replace("<ar:ApplicationResponse>", "</ar:ApplicationResponse>");
    
            var xmlInput = XDocument.Parse(xml);
            var documento = LeerXML.Procesar(xmlInput);
    
            Console.WriteLine("UBLVersionID: " +documento.UBLVersionID.ToString());
            Console.WriteLine("CustomizationID: " + documento.CustomizationID.ToString());
            Console.WriteLine("ID: " + documento.ID.ToString());
            Console.WriteLine("IssueDate: " + documento.IssueDate.ToShortDateString());
            Console.WriteLine("IssueTime: " + documento.IssueTime.ToShortTimeString());
            Console.WriteLine("ResponseDate: " + documento.ResponseDate.ToShortDateString());
            Console.WriteLine("ResponseTime: " + documento.ResponseTime.ToShortTimeString());
            int i = 0;
            foreach (var n in documento.Notes) {
                i++;
                Console.WriteLine("Nota" + i.ToString() + ": " + n);
            }
    
            Console.ReadLine();
        }
    

    Create the LeerXML class:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Xml.Linq;
    
    namespace LeerXMLConNamespace
    
    {
        public class LeerXML
    {
    
        public static Documento Procesar(XDocument xml)
        {
            try
            {
    
                XNamespace cbc = @"urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2";
    
                var doc = new Documento();
    
    
                //UBLVersionID
                var UBLVersionID = (from x in xml.Descendants(cbc + "UBLVersionID")
                                    select x).FirstOrDefault();
                if (UBLVersionID != null)
                {
                    doc.UBLVersionID = decimal.Parse(UBLVersionID.Value == null ? "0.0" : UBLVersionID.Value.ToString());
                }
                //CustomizationID
                var CustomizationID = (from x in xml.Descendants(cbc + "CustomizationID")
                                    select x).FirstOrDefault();
                if (CustomizationID != null)
                {
                    doc.CustomizationID = decimal.Parse(CustomizationID.Value == null ? "0.0" : CustomizationID.Value.ToString());
                }
                //CustomizationID
                var ID = (from x in xml.Descendants(cbc + "ID")
                                       select x).FirstOrDefault();
                if (ID != null)
                {
                    doc.ID = Int64.Parse(ID.Value == null ? "0" : ID.Value.ToString());
                }
                //IssueDate
                var IssueDate = (from x in xml.Descendants(cbc + "IssueDate")
                          select x).FirstOrDefault();
                if (IssueDate != null)
                {
                    doc.IssueDate = DateTime.Parse(IssueDate.Value == null ? "1900-01-01" : IssueDate.Value.ToString());
                }
                //IssueTime
                var IssueTime = (from x in xml.Descendants(cbc + "IssueTime")
                                 select x).FirstOrDefault();
                if (IssueTime != null)
                {
                    doc.IssueTime = DateTime.Parse(IssueTime.Value == null ? "1900-01-01" : "1900-01-01 " + IssueTime.Value.ToString());
                }
                //IssueDate
                var ResponseDate = (from x in xml.Descendants(cbc + "ResponseDate")
                                 select x).FirstOrDefault();
                if (ResponseDate != null)
                {
                    doc.ResponseDate = DateTime.Parse(ResponseDate.Value == null ? "1900-01-01" : ResponseDate.Value.ToString());
                }
                //IssueTime
                var ResponseTime = (from x in xml.Descendants(cbc + "ResponseTime")
                                 select x).FirstOrDefault();
                if (ResponseTime != null)
                {
                    doc.ResponseTime = DateTime.Parse(ResponseTime.Value == null ? "1900-01-01" : "1900-01-01 " + ResponseTime.Value.ToString());
                }
                //IssueTime
                var Notes = (from x in xml.Descendants(cbc + "Note")
                                    select x).ToList();
                doc.Notes = new List<string>();
                foreach (var n in Notes)
                {
                    doc.Notes.Add(n.Value ?? "");
                }             
    
    
                return doc;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
    }
    

    Create the Document entity, which contains the XML data:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace LeerXMLConNamespace
    {
        public class Documento
        {
            public decimal UBLVersionID { get; set; }
            public decimal CustomizationID { get; set; }
            public Int64 ID { get; set; }
            public DateTime IssueDate { get; set; }
            public DateTime IssueTime { get; set; }
            public DateTime ResponseDate { get; set; }
            public DateTime ResponseTime { get; set; }
            public List<string> Notes { get; set; }
        }
    }
    

    Finally, I'll add the example application I made: Sample read XML

        
    answered by 28.12.2016 / 21:30
    source