locked
XML File Writing Problem RRS feed

  • Question

  • hi..
     i m creating a xml file.. everything is fine but a problem is that after creation the file contain URI with every element tag. can anyone tell what the mistake.
    XmlTextWriter writer = new XmlTextWriter(file_Name, System.Text.Encoding.UTF8);
                        writer.Formatting = Formatting.Indented;
                        writer.WriteProcessingInstruction("xml", "version='1.0' encoding='utf-8'");
                        writer.WriteStartElement("Abc", "'http://labc'  xmlns:abc=xyz'");
                        writer.Close();
                        xmlDoc.Load(file_Name);
    XmlNode root = xmlDoc.DocumentElement;
    XmlElement title = xmlDoc.CreateElement("title");
                    XmlElement str = xmlDoc.CreateElement("string");
                    str.SetAttribute("type", "string");
                    str.InnerText = "ABC";
    XmlElement general = xmlDoc.CreateElement("general");
                    general.AppendChild(title);
    root.appendchild(general);

    output:
    <?xml version="1.0" encoding="utf-8>
    <Abc xmlns="'http://labc' xmlns:abc='xyz'">
    <general xmlns="">
    ....
    Thursday, November 26, 2009 5:43 AM

Answers

  • Hi,

    To produce the element you specified you need this piece of code:
                    XNamespace abcd = XNamespace.Get("http://Abcd.com");
                    XNamespace xsi = XNamespace.Get(@"http://abcd.com\aaa");
                    XElement xyz = new XElement(abcd + "xyz",
                        new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName),
                        new XAttribute(xsi + "schemaLocation", @"http:\\abcdef.com\loc"));
                    Console.WriteLine(xyz);
    But please read something about XML namespaces as you're using the wrong terms to describe what you want. There are multiple places to read about them, you can start for example on wikipedia: http://en.wikipedia.org/wiki/XML_namespace

    Thanks,
    Vitek Karas [MSFT]
    Tuesday, December 1, 2009 12:52 PM
    Moderator

All replies

  • It is not clear what you want to achieve. Please post a sample of the XML you want to create. Also why do you use both XmlTextWriter and XmlDocument? One should suffice.
    MVP XML My blog
    Thursday, November 26, 2009 11:36 AM
  • Hi,

    The "URI" is called XML namespace. The WriteStartElement call you use specifies that it should write an element "Abc" in namespace "'http://labc' xmlns:abc='xyz'" (yes, the whole thing!). Now I'm having hard time guessing what XML namespace you want the Abc element to be in. For example if I want <abc:Abc xmlns:abc='xyz'/> as the result then you should call WriteStartElement("abc", "Abc", "xyz"). The writer will infer the xmlns attribute for you (as otherwise the prefix abc would not be declared, so it declares it for you).

    After that you seem to want to add element "general" as a child of the "Abc" and I assume you want the general element to be in the same namespace as Abc. If so, you need to specify that the element general should be in the namespace "xyz", otherwise it's created in the empty namespace (that's why the xmlns="" is inserted there, so that the namespace of the general elements really is empty).
    To do that you need to call xmlDoc.CreateElement("general", "xyz");
    Note that if you want the general to also use the prefix abc, then you should call xmlDoc.CreateElement("abc:general", "xyz").

    Few other notes:
    Please do not use XmlTextWriter if you're in .NET 2.0 or higher. Use XmlWriter and XmlWriter.Create instead.
    I don't understand why you're writing the root element using XmlWriter and then loading that into XmlDocument and creating the rest of the document using the XmlDocument APIs. I would choose one API and stick with it - it's faster and easier to code/read.

    If you're using .NET 3.5 there's a new API called LINQ to XML, which makes the creation of such XML much easier. This piece of LINQ to XML code will create the same document as your code:
                XNamespace abc = XNamespace.Get("xyz");
                XDocument document = new XDocument(
                    new XElement(abc + "Abc",
                        // Need to add the xmlns:abc='xyz' attribute so that the prefix 'abc' will be used
                        //   otherwise an empty prefix would be used by default
                        new XAttribute(XNamespace.Xmlns + "abc", abc.NamespaceName),
                        new XElement(abc + "general",
                            new XElement(abc + "title",
                                new XElement(abc + "string",
                                    new XAttribute("type", "string"),
                                    "ABC")
                            )
                        )
                    )
                );
    
                document.Save(file_Name); // UTF8 is used by default
    

    This will produce XML file like this:
    <abc:Abc xmlns:abc="xyz">
      <abc:general>
        <abc:title>
          <abc:string type="string">ABC</abc:string>
        </abc:title>
      </abc:general>
    </abc:Abc>


    Thanks,
    Vitek Karas [MSFT]
    Thursday, November 26, 2009 11:44 AM
    Moderator
  • i want uri in this format.
    <xyz xmlns="http://Abcd.com" xmlns:xsi="http://abcd.com\aaa" xsi:schemaLocation="http:\\abcdef.com\loc">
    Tuesday, December 1, 2009 4:44 AM
  • Hi,

    To produce the element you specified you need this piece of code:
                    XNamespace abcd = XNamespace.Get("http://Abcd.com");
                    XNamespace xsi = XNamespace.Get(@"http://abcd.com\aaa");
                    XElement xyz = new XElement(abcd + "xyz",
                        new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName),
                        new XAttribute(xsi + "schemaLocation", @"http:\\abcdef.com\loc"));
                    Console.WriteLine(xyz);
    But please read something about XML namespaces as you're using the wrong terms to describe what you want. There are multiple places to read about them, you can start for example on wikipedia: http://en.wikipedia.org/wiki/XML_namespace

    Thanks,
    Vitek Karas [MSFT]
    Tuesday, December 1, 2009 12:52 PM
    Moderator