0

Good afternoon. I have issues adding a especific atributes to 2 starter tags in a xml using c#. The 2 tags should look like this:

<EnvioDTE xmlns="http://www.sii.cl/SiiDte"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          version="1.0"
          xsi:schemaLocation="http://www.sii.cl/SiiDte EnvioDTE_v10.xsd">

<SetDTE xmlns="http://www.sii.cl/SiiDte"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        ID="SetDoc">

But the result is this:

<EnvioDTE xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://www.sii.cl/SiiDte EnvioDTE_v10.xsd"
          version="1.0"
          xmlns="http://www.sii.cl/SiiDte">

  <SetDTE xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          ID="SetDoc">

The code i'm using to generate the xml is this:

public void dtebasico()
{
    var doc = new XDocument(new XDeclaration("1.0", "ISO-8859-1", ""));

    using (XmlWriter writer = doc.CreateWriter())
    {
        string ah = "http://www.sii.cl/SiiDte";
        writer.WriteStartDocument();
        writer.WriteStartElement("EnvioDTE", ah);

        writer.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
        writer.WriteAttributeString("xsi", "schemaLocation", null, "http://www.sii.cl/SiiDte EnvioDTE_v10.xsd");
        writer.WriteAttributeString("version", "1.0");

        writer.WriteStartElement("SetDTE", "http://www.sii.cl/SiiDte");
        writer.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
        writer.WriteAttributeString("ID", "SetDoc");
        ...

    }
    doc.Save("dte" + folio + ".xml");

}

Can you help me please? I'm really desperate.

5
  • Aren't the EnvioDTE the same, just with a different order of attributes? (which won't matter) Commented Feb 29, 2016 at 21:04
  • Child elements inherit the namespace from the parent if it was declared using the xmlns attribute. In your example, because SetDTE belongs to the same namespace as the parent XmlWriter did not write the xmlns attribute since it is not needed. Commented Feb 29, 2016 at 21:29
  • Ok i understand that. Commented Feb 29, 2016 at 21:56
  • But i want to that the xmlns atribute in the end of EnvioDte will be at the beginning of the tag, not at the end. Commented Feb 29, 2016 at 21:58
  • 1
    I don't think you can enforce that but at the same time it shouldn't matter any compliant Xml parser should be able to process the document correctly regardless of the position of the xml attribute. Commented Mar 1, 2016 at 3:52

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.