0

I am working with an xml specification from outside my organization. I need to generate an xml file that complies with the spec. The file will not validate properly unless the default namespace xmlns="ndnqi" is listed first on the root element like so:

<HospYearQtr xmlns="ndnqi" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="ndnqi NDNQIV10.xsd">

I generated classes from the XSD using xsd.exe and when I serialize to xml it puts the default namespace xmlns at the end of the root element.

Is there any way to force the default namespace to be listed first?

4

1 回答 1

0

理想情况下,让他们修复损坏的解析器。XSD 描述的 XML 中的属性不是有序项。

除非您在生成内容后可能需要执行正则表达式替换之类的操作。下面的代码应该是一个有用的起点。

Regex moveDefaultNamespace = new Regex(@"(<\w+)\s+(.*?)(xmlns="".+?"")\s+(.*?>)");
string xmlText = "<HospYearQ1tr xmlns=\"ndnqi\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"ndnqi NDNQIV10.xsd\">";
string reordered = moveDefaultNamespace.Replace(xmlText, "$1 $2 $3 $4");
于 2013-07-10T15:47:58.580 回答