Asked  7 Months ago    Answers:  5   Viewed   27 times

How can I generate valid XML in C#?



It depends on the scenario. XmlSerializer is certainly one way and has the advantage of mapping directly to an object model. In .NET 3.5, XDocument, etc. are also very friendly. If the size is very large, then XmlWriter is your friend.

For an XDocument example:

    new XElement("Foo",
        new XAttribute("Bar", "some & value"),
        new XElement("Nested", "data")));

Or the same with XmlDocument:

XmlDocument doc = new XmlDocument();
XmlElement el = (XmlElement)doc.AppendChild(doc.CreateElement("Foo"));
el.SetAttribute("Bar", "some & value");
el.AppendChild(doc.CreateElement("Nested")).InnerText = "data";

If you are writing a large stream of data, then any of the DOM approaches (such as XmlDocument/XDocument, etc.) will quickly take a lot of memory. So if you are writing a 100 MB XML file from CSV, you might consider XmlWriter; this is more primitive (a write-once firehose), but very efficient (imagine a big loop here):

XmlWriter writer = XmlWriter.Create(Console.Out);
writer.WriteAttributeString("Bar", "Some & value");
writer.WriteElementString("Nested", "data");

Finally, via XmlSerializer:

public class Foo
    public string Bar { get; set; }
    public string Nested { get; set; }
Foo foo = new Foo
    Bar = "some & value",
    Nested = "data"
new XmlSerializer(typeof(Foo)).Serialize(Console.Out, foo);

This is a nice model for mapping to classes, etc.; however, it might be overkill if you are doing something simple (or if the desired XML doesn't really have a direct correlation to the object model). Another issue with XmlSerializer is that it doesn't like to serialize immutable types : everything must have a public getter and setter (unless you do it all yourself by implementing IXmlSerializable, in which case you haven't gained much by using XmlSerializer).

Tuesday, June 1, 2021
answered 7 Months ago

Because your document has default namespace declared in the most outer element you have to repeat that namespace on every child element to avoid adding additional empty one.

Change request and param elements declaration to contain "" namespace

XmlElement request = (XmlElement)body.AppendChild(doc.CreateElement("findItemsByKeywordsRequest", ""));
XmlElement param = (XmlElement)request.AppendChild(doc.CreateElement("keywords", ""));

With these changes your code produces following XML:

<soap:Envelope xmlns="" xmlns:soap="">
    <soap:Header />
            <keywords>harry potter phoenix</keywords>
Saturday, August 7, 2021
answered 4 Months ago

I believe you can use XmlSerializer's UnknownAttribute, UnknownElement, etc. events to trap such cases.

Wednesday, September 1, 2021
answered 3 Months ago

You can use either SHCreateMemStream or CreateStreamOnHGlobal to create an memory stream. Here is the sample code for your reference:

CComPtr<IStream> spMemoryStream;
CComPtr<IXmlWriter> spWriter;
CComPtr<IXmlWriterOutput> pWriterOutput;

// Opens writeable output stream.
spMemoryStream.Attach(::SHCreateMemStream(NULL, 0));
if (spMemoryStream == NULL)
    return E_OUTOFMEMORY;

// Creates the xml writer and generates the content.
CHKHR(::CreateXmlWriter(__uuidof(IXmlWriter), (void**) &spWriter, NULL));
    NULL, L"utf-16", &pWriterOutput));
CHKHR(spWriter->SetProperty(XmlWriterProperty_Indent, TRUE));
CHKHR(spWriter->WriteStartElement(NULL, L"root", NULL));
CHKHR(spWriter->WriteCData(L"This is CDATA text."));

// Allocates enough memeory for the xml content.
STATSTG ssStreamData = {0};
CHKHR(spMemoryStream->Stat(&ssStreamData, STATFLAG_NONAME));
SIZE_T cbSize = ssStreamData.cbSize.LowPart;
LPWSTR pwszContent = (WCHAR*) new(std::nothrow) BYTE[cbSize + sizeof(WCHAR)];
if (pwszContent == NULL)
    return E_OUTOFMEMORY;

// Copies the content from the stream to the buffer.
position.QuadPart = 0;
CHKHR(spMemoryStream->Seek(position, STREAM_SEEK_SET, NULL));
SIZE_T cbRead;
CHKHR(spMemoryStream->Read(pwszContent, cbSize, &cbRead));
pwszContent[cbSize / sizeof(WCHAR)] = L'';

wprintf(L"%s", pwszContent);

One pretty thing about XmlLite is it works with IStream interface. It really doesn't care what exactly the stream looks like behind the scene.

Wednesday, October 27, 2021
Nenad Dordevic
answered 1 Month ago

The attributes "xmlns:wnio" and "xmlns:xf" are attributes like any other. Simply add them to the XmlElement that you would like these XML Namespaces to scope to.

The following snippet produces almost exactly what you want:

XmlDocument document = new XmlDocument();
document.AppendChild(document.CreateElement("wnio", "element", "somuri"));
document.DocumentElement.SetAttribute("xmlns:xf", "abcd");
document.DocumentElement.AppendChild(document.CreateElement("xf", "nestedelement", "abcd"));
Saturday, November 20, 2021
answered 2 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :