我需要使用LINQ to XML和C#重现以下XML头:
<ns0:Subject_Sample xmlns:ns0="fhrb" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="fhrb file:FHRB_NEW_SUBJECT_SAMPLE.xsd" >
第一个问题是,当我调整.NET帮助示例时,事情开始从我的标题中消失。例如:
XElement myTree = new XElement(ns0 + "Subject_Sample",
new XAttribute(XNamespace.Xmlns + "ns0", "http://www.adventure-works.com" )
);
给出了我需要的东西
<ns0:Subject_Sample xmlns:ns0="http://www.adventure-works.com">
但如果我将XAttribute参数从Web URL更改为字符串(例如“fhrb”),则由于某种原因“ns0:”从标记中消失(ns0:Subject_Sample变为Subject_Sample)。
然后,我决定尝试通过以下代码创建schemaLocation属性:
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"));
但我得到的结果如下,奇怪的p1出现了。
<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">
问题是:如何通过LINQ to XML重现所需的头文件格式?这些属性的出现/消失/命名背后的逻辑是什么?
答案 0 :(得分:0)
试试这个:
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
XNamespace ns0 = "fhrb";
XElement myTree = new XElement(ns0 + "Subject_Sample",
new XAttribute(XNamespace.Xmlns + "xsi", xsi),
new XAttribute(XNamespace.Xmlns + "ns0", ns0),
new XAttribute(xsi + "schemaLocation", "fhrb file:FHRB_NEW_SUBJECT_SAMPLE.xsd")
);