Insert prefix in a Xml string [duplicate]

I need to be able to write this Xml string:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" 
            xmlns:a="http://www.w3.org/2005/08/addressing" 
            xmlns:fse="http://www.sist.puglia.it/Schemas/PDD_SIST/SCATEL/FSE/" 
            xmlns:u="http://docs.oasisopen.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">

To do this I use this code:

XNamespace myns = "s";

XDocument oXml = new XDocument(new XDeclaration("1.0", "UTF-8", ""));

XElement oRoot = new XElement("Envelope",
    new XAttribute(XNamespace.Xmlns + "s", "http://schemas.xmlsoap.org/soap/envelope/"),
    new XAttribute(XNamespace.Xmlns + "a", "http://www.w3.org/2005/08/addressing"),
    new XAttribute(XNamespace.Xmlns + "fse", "http://www.sist.puglia.it/Schemas/PDD_SIST/SCATEL/FSE/"),
    new XAttribute(XNamespace.Xmlns + "u", "http://docs.oasisopen.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd")
);


oXml.Add(oRoot);

This is the result, which is fine but the initial s: is missing before Envelope and I don’t know how to insert it.

<Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" 
          xmlns:a="http://www.w3.org/2005/08/addressing" 
          xmlns:fse="http://www.sist.puglia.it/Schemas/PDD_SIST/SCATEL/FSE/" 
          xmlns:u="http://docs.oasisopen.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" />

I tried putting an XNamespace at the beginning of the XElement, like this:

XElement oRoot = new XElement(myns + "Envelope",
    new XAttribute(XNamespace.Xmlns + "s", "http://schemas.xmlsoap.org/soap/envelope/"),
    new XAttribute(XNamespace.Xmlns + "a", "http://www.w3.org/2005/08/addressing"),
    new XAttribute(XNamespace.Xmlns + "fse", "http://www.sist.puglia.it/Schemas/PDD_SIST/SCATEL/FSE/"),
    new XAttribute(XNamespace.Xmlns + "u", "http://docs.oasisopen.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd")
);

But the result is this:

<Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" 
          xmlns:a="http://www.w3.org/2005/08/addressing" 
          xmlns:fse="http://www.sist.puglia.it/Schemas/PDD_SIST/SCATEL/FSE/" 
          xmlns:u="http://docs.oasisopen.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" 
          xmlns="s" />

I don’t know what else to try, can you help me?

EDIT: As I wrote in the question I have already tried to insert the namespace before Envelope, as suggested in the post but it doesn’t work for me.

2

Namespace prefixes only carry meaning within their own scope and can be arbitrarily selected. Stop trying to manually control these and the XML that’s output should be correct. That is, both of these elements are the same:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" />
<foo:Envelope xmlns:foo="http://schemas.xmlsoap.org/soap/envelope/" />

So just declare the actual namespaces you want to work with in your code and let the XDocument etc insert the required namespace attributes as required:

XNamespace SchemaNs = "http://schemas.xmlsoap.org/soap/envelope/";
XNamespace AddressingNs = "http://www.w3.org/2005/08/addressing";
XNamespace PddNs = "http://www.sist.puglia.it/Schemas/PDD_SIST/SCATEL/FSE/";
XNamespace OasisNs = "http://docs.oasisopen.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd";

XDocument oXml = new XDocument(new XDeclaration("1.0", "UTF-8", ""));

XElement oRoot = new XElement(SchemaNs + "Envelope");

//Use other namespace objects as appropriate whilst building child elements, etc, as SchemaNs is used above

oXml.Add(oRoot);

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật