0

I am trying to add the following attribute to a node using an XmlWriter but nothing seems to work, does anyone have any ideas?

<news:news>

I am trying to achieve the following:

 <url>
    <loc>http://www.example.org/business/article55.html</loc>
    <news:news>
      <news:publication>
        <news:name>The Example Times</news:name>
        <news:language>en</news:language>
      </news:publication>
      <news:access>Subscription</news:access>
      <news:genres>PressRelease, Blog</news:genres>
      <news:publication_date>2008-12-23</news:publication_date>
      <news:title>Companies A, B in Merger Talks</news:title>
      <news:keywords>business, merger, acquisition, A, B</news:keywords>
      <news:stock_tickers>NASDAQ:A, NASDAQ:B</news:stock_tickers>
    </news:news>
  </url>

Thanks

7
  • 3
    That's not an attribute - it's the start of an element. You need to be very precise in your terminology here. Can you post a complete example of what you're trying to achieve? Commented Nov 30, 2012 at 15:27
  • ok, the start of the element with the extra part on the end Commented Nov 30, 2012 at 15:27
  • 1
    What do you mean by "with the extra part on the end"? Again, a complete example would really help. If you're vague with the question, we're just not going to be able to help you. Please read tinyurl.com/so-list Commented Nov 30, 2012 at 15:28
  • I have added it to the description what I am trying to achieve Commented Nov 30, 2012 at 15:29
  • 1
    Right. So again, please post what you've tried. Read the link I gave you before - we shouldn't be having to ask this many questions... Commented Nov 30, 2012 at 15:35

2 Answers 2

1

Take a look at the following link for how to handle namespaces with the XmlWriter

Namespace Handling in the XmlWriter

You can manually write out the namespace declaration using the WriteAttributeString method, and then use the WriteStartElement(String, String) overload to associate future elements with that namespace, like so

writer.WriteStartElement("root");
writer.WriteAttributeString("xmlns", "x", null, "urn:1");
writer.WriteStartElement("item", "urn:1");
writer.WriteEndElement();
writer.WriteStartElement("item", "urn:1");
writer.WriteEndElement();
writer.WriteEndElement();
Sign up to request clarification or add additional context in comments.

1 Comment

I cant see anywhere on that page or this code how to achieve the code
0
    using System;
    using System.Xml;

    class Program
    {
        static void Main(string[] args)
        {
            XmlWriter writer = new XmlTextWriter(Console.Out);
            writer.WriteStartElement("root");
            writer.WriteAttributeString("news", "http://www.stackoverflow.com");
            writer.WriteStartElement("news:news");
            writer.WriteStartElement("news:publication");
            writer.WriteElementString("news:name", "The Example Times");
            writer.WriteElementString("news:language", "en");
            // etc
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndElement();
            Console.ReadKey(true);
        }
    }

2 Comments

I did this and got the following error: Invalid name character in 'news:news'. The ':' character, hexadecimal value 0x3A, cannot be included in a name
thats great but it doesn't work when you have the items inside another node

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.