Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make an XML header with given attributes in LINQ-to-XML?
    primarykey
    data
    text
    <p>I am in need to reproduce the following XML header using LINQ to XML and C#: </p> <pre><code>&lt;ns0:Subject_Sample xmlns:ns0="fhrb" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="fhrb file:FHRB_NEW_SUBJECT_SAMPLE.xsd" &gt; </code></pre> <p>First problem is that when I adapt .NET help example, things start disappear from my header. For example:</p> <pre><code>XElement myTree = new XElement(ns0 + "Subject_Sample", new XAttribute(XNamespace.Xmlns + "ns0", "http://www.adventure-works.com" ) ); </code></pre> <p>gives sort of what I need</p> <pre><code> &lt;ns0:Subject_Sample xmlns:ns0="http://www.adventure-works.com"&gt; </code></pre> <p>but if I change XAttribute parameter from web URL to string (eg "fhrb") then for some reason "ns0:" disappears from the tag (ns0:Subject_Sample becomes just Subject_Sample).</p> <p>Then, I decided to try to make schemaLocation attribute by the following code:</p> <pre><code>XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance"; XNamespace ns0 = "http://www.adventure-works.com"; XElement myTree = new XElement(ns0 + "Subject_Sample", new XAttribute(XNamespace.Xmlns + "ns0", "http://www.adventure-works.com" ), new XAttribute(xsi+"schemaLocation", "fhrb file:/fhrb.xsd")); </code></pre> <p>but the result i got was as follows, strange p1 appeared.</p> <pre><code> &lt;ns0:Subject_Sample xmlns:ns0="http://www.adventure-works.com" p1:schemaLocation="fhrb file:/fhrb.xsd" xmlns:p1="http://www.w3.org/2001/XMLSchema-instance"&gt; </code></pre> <p>Question is: how I can reproduce the needed format of header by means of LINQ to XML? And what is the logic behind the appearance/disappearance/naming of these attributes?? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload