3

I want to make xml element like this:

<ElementName Type="FirstAttribute" Name="SecondAttribute">Value</Atrybut>

Now I'm doing this in this way:

XmlNode xmlAtrybutNode = xmlDoc.CreateElement("ElementName ");

_xmlAttr = xmlDoc.CreateAttribute("Type");
_xmlAttr.Value = "FirstAttribute";
xmlAtrybutNode.Attributes.Append(_xmlAttr);

_xmlAttr = xmlDoc.CreateAttribute("Name");
_xmlAttr.Value = "SecondAttribute";
xmlAtrybutNode.Attributes.Append(_xmlAttr);


xmlAtrybutNode.InnerText = !string.IsNullOrEmpty(Value) 
    ? SetTextLength(Name, ValueLength) 
    : string.Empty;

Value is input variable in method. Is there possibility to make this in another way? More efficiently? Can I use xmlWriter? Now i'm using xmlDocument.

3
  • if you are trying to serialize objects from your app take a look at XmlSerializer: msdn.microsoft.com/en-us/library/… Commented Jan 21, 2012 at 17:12
  • What you are trying to generate looks pretty broken XML. Commented Jan 21, 2012 at 17:12
  • Why pretty broken? I'm doing this according to specification. I have sample xml output and I have to produce that xml. Commented Jan 21, 2012 at 17:19

4 Answers 4

6

You can use Linq to XML.

Basically

        XDocument doc = new XDocument();
        doc.Add(
            new XElement("ElementName", "Value",
                new XAttribute("Type", "FirstAttribute"),
                new XAttribute("Name", "SecondAttribute")));

will give this xml document

<ElementName Type="FirstAttribute" Name="SecondAttribute">Value</ElementName>
Sign up to request clarification or add additional context in comments.

Comments

4

How about tweaking your existing code:

XmlElement el = xmlDoc.CreateElement("ElementName");
el.SetAttribute("Type", "FirstAttribute");
el.SetAttribute("Name", "SecondAttribute");
el.InnerText = ...;

Additional thoughts:

  • XElement
  • XmlSerializer (from a class instance)

Comments

4

If you’re on .NET 3.5 (or later), you could use LINQ to XML. Make sure that the System.Xml.Linq assembly is referenced, and that you have a using directive for its eponymous namespace.

XDocument document = new XDocument(
    new XElement("ElementName",
        new XAttribute("Type", "FirstAttribute"),
        new XAttribute("Name", "SecondAttribute"),
        value));

If you subsequently want to write the XDocument to a target, you can use its Save method. For debugging, it’s useful to call its ToString method, which returns its XML representation as a string.

Edit: Replying to comment:

If you need to convert the XDocument created above into an XmlDocument instance, you may use code similar to the following:

XmlDocument xmlDocument = new XmlDocument();
using (XmlReader xmlReader = document.CreateReader())
    xmlDocument.Load(xmlReader);

1 Comment

Can i add this document as appendChild to xmlDocument?
3

What about using LINQ to XML as in this article. That can be very elegant - it can all be done on one line.

 XDocument doc = new XDocument(
      new XDeclaration("1.0", "utf-8", "yes"),
      new XElement("element",
            new XAttribute("attribute1", "val1"),
            new XAttribute("attribute2", "val2"),
       )
);

Comments

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.