Output XML file using XElement

S

standerby

I am writing a XML file suing XElement. How can I output a file like
the following? I got an exception if if I use ':' in the element name
or attribute name.

Thanks,

<?xml version="1.0" encoding="UTF-8"?>
<epoStick out tongueolicySchema xmlns:epo="epo-policy"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</epoStick out tongueolicySchema>
 
S

standerby

What is the code you're trying to use?  It's hard to say what you're doing  
wrong if you don't say what you're doing.

If I've read the docs correctly, you should be able to create a proper  
XName instance by combining an XNamespace instance with a string:

     XNamespace ns = "xmlns";
     XName nameEpo = ns + "epo",
         nameXsi = ns + "xsi";

Then you'd pass the appropriate XName to the XElement.SetAttribute()  
method (or just create the XName in the method call itself, rather than  
putting it in a variable).

If that doesn't address your question, you should probably be more  
specific.  :)

Pete

Pete,
I have tried similar code to yours before I post here and it doesn't
work very well. Your code will output like this
<EPOPolicySchema p1:epo="mcafee-epo-policy" p1:xsi="http://www.w3.org/
2001/XMLSchema-instance" xmlns:p1="xlmns">
What I need is the exact format like <epo:EPOPolicySchema
xmlns:epo="mcafee-epo-policy" xmlns:xsi="http://www.w3.org/2001/
XMLSchema-instance"> because it is vendor specific format. Do you know
how to make change? Thanks.
 
J

Jeroen Mostert

standerby said:
I am writing a XML file suing XElement. How can I output a file like
the following? I got an exception if if I use ':' in the element name
or attribute name.

Thanks,

<?xml version="1.0" encoding="UTF-8"?>
<epoStick out tongueolicySchema xmlns:epo="epo-policy"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</epoStick out tongueolicySchema>

Stick out tongue, eh? :)

XNamespace epo = "mcafee-epo-policy";
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
XElement epoPolicySchema = new XElement(epo + "EPOPolicySchema",
new XAttribute(XNamespace.Xmlns + "epo", epo.NamespaceName),
new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName)
);

This is not terribly elegant or intuitive, but then, neither are XML namespaces.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top