Create XML with specific structure

0

I need to generate an xml with a somewhat complicated structure

First block:

<rule id="Main">
    <item>
      YO <ruleref uri="#Nombre" />
      CON NUMERO DE TELEFONO CELULAR<ruleref uri="#Numero" />
      EXPRESO MI VOLUNTAD<ruleref uri="#Voluntad" />
      MI CUENTA INDIVIDUAL<ruleref uri="#Tramite" />
      Y HAGO ENTREGA<ruleref uri="#Recibe" />
      AL AGENTE PROMOTOR<ruleref uri="#Agente" />
      CON NUMERO<ruleref uri="#Numero1" />
      QUIEN ME PROPORCIONO<ruleref uri="#Propociono" />
      DE LAS IMPLICACIONES<ruleref uri="#Implicaciones" /></item>
  </rule>
  <rule id="Nombre" scope="public">
    <one-of>
      <item>GERARDO TOLEDO SANCHEZ</item>
    </one-of>
  </rule>

Second block:

<rule id="Numero" scope="public">
    <one-of>
      <item>5566778899</item>
    </one-of>   </rule>   <rule id="Voluntad" scope="public">
    <one-of>
      <item>DE TRANSPASAR</item>
    </one-of>   </rule>

With the second block I have no problem generating it:

XDocument miXML = XDocument.Load(pathDataFile); //Cargamos
            miXML.Root.Add(   //Obtiene la raiz del documento (Empleados)                         
             new XElement("rule",
             new XAttribute("id", "Numero"),
             new XAttribute("scope", "public"),
             new XElement("one-of", new XElement("item", "5566778899"))
                          ));
            miXML.Save(pathDataFile);

But I just can not make the first block generate, I tried assigning a new element within the text but it does not generate

Will they have any idea how to generate something similar? All help is welcome

    
asked by Alejandro Reyes 10.07.2018 в 00:15
source

2 answers

0

Let's see if the following example serves you. A node with mixed content is created:

using System;
using System.Xml;

namespace xmlMixedContent
{
    class Program
    {
        static void Main(string[] args)
        {
            //salida del resultado por la consola
            XmlTextWriter writer = null;
            writer = new XmlTextWriter(Console.Out);

            writer.Formatting = Formatting.Indented;

            //creación de un elemento 
            writer.WriteStartElement("libro");

            //elemento con contenido mixto
            writer.WriteStartElement("titulo");
            writer.WriteString("El ingenioso hidalgo ");
            writer.WriteStartElement("b");
            writer.WriteString("Don Quijote de la Mancha");
            writer.WriteEndElement();
            writer.WriteEndElement();

            //cierre del elemento libro
            writer.WriteEndElement();

            writer.Close();

            Console.Read();
        }           }
}

The result of the execution is:

<libro>
    <titulo>El ingenioso hidalgo <b>Don Quijote de la Mancha</b></titulo>
</libro>
    
answered by 10.07.2018 / 22:26
source
0

The XML file you propose (and I stick to your first and second block)

<?xml version="1.0" encoding="UTF-8"?>
<rule id="Main" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\TMP\XML\AAAAA.xsd">
    <item>
      YO <ruleref uri="#Nombre"/>
      CON NUMERO DE TELEFONO CELULAR<ruleref uri="#Numero"/>
      EXPRESO MI VOLUNTAD<ruleref uri="#Voluntad"/>
      MI CUENTA INDIVIDUAL<ruleref uri="#Tramite"/>
      Y HAGO ENTREGA<ruleref uri="#Recibe"/>
      AL AGENTE PROMOTOR<ruleref uri="#Agente"/>
      CON NUMERO<ruleref uri="#Numero1"/>
      QUIEN ME PROPORCIONO<ruleref uri="#Propociono"/>
      DE LAS IMPLICACIONES<ruleref uri="#Implicaciones"/>
    </item>
    <rule id="Nombre" scope="public">
        <one-of>
            <item>GERARDO TOLEDO SANCHEZ</item>
        </one-of>
    </rule>
</rule>

is well formed and is valid, as long as it fits into a scheme such as:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    <xs:element name="item">
        <xs:complexType mixed="true">
            <xs:choice minOccurs="0" maxOccurs="unbounded">
                <xs:element ref="ruleref"/>
            </xs:choice>
        </xs:complexType>
    </xs:element>
    <xs:element name="one-of">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="item"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="rule">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="item" minOccurs="0"/>
                <xs:element ref="rule" minOccurs="0"/>
                <xs:element ref="one-of" minOccurs="0"/>
            </xs:sequence>
            <xs:attribute name="id" use="required">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:enumeration value="Main"/>
                        <xs:enumeration value="Nombre"/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:attribute>
            <xs:attribute name="scope">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:enumeration value="public"/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:attribute>
        </xs:complexType>
    </xs:element>
    <xs:element name="ruleref">
        <xs:complexType>
            <xs:attribute name="uri" use="required">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:enumeration value="#Agente"/>
                        <xs:enumeration value="#Implicaciones"/>
                        <xs:enumeration value="#Nombre"/>
                        <xs:enumeration value="#Numero"/>
                        <xs:enumeration value="#Numero1"/>
                        <xs:enumeration value="#Propociono"/>
                        <xs:enumeration value="#Recibe"/>
                        <xs:enumeration value="#Tramite"/>
                        <xs:enumeration value="#Voluntad"/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:attribute>
        </xs:complexType>
    </xs:element>
</xs:schema>

Another thing is that it is a bit convoluted, hence your problem to create it via programming. You yourself say that the second block you generate without problem. Why do not you think a bit about improving the first block and doing it, let's say, more XML style?

    
answered by 10.07.2018 в 16:04