Creating XDocument with xsi:schemaLocation namespace

前端 未结 1 1082
暖寄归人
暖寄归人 2020-12-15 18:31

I need to create the following XML and I\'m trying to do this using XDocument. However, I\'m having trouble specifying the name spaces.



        
相关标签:
1条回答
  • 2020-12-15 19:17

    This is because the xsi is a namespace in itself. You would need to do something like:

            XNamespace xmlns = XNamespace.Get("http://ns.hr-xml.org/2007-04-15");
            XNamespace xsi = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance");
            XNamespace schemaLocation = XNamespace.Get("http://ns.hr-xml.org/2007-04-15 http://ns.hr-xml.org/2_5/HR-XML-2_5/StandAlone/AssessmentOrderRequest.xsd");
    
            return new XDocument(
                new XElement(xmlns + "AssessmentOrderRequest",
                    new XAttribute(XNamespace.Xmlns + "xsi", xsi),
                    new XAttribute(xsi + "schemaLocation", schemaLocation)
                )
            );
    

    EDIT: Updated with final code that I used to solve the problem. With thanks to the original answer from James.

    0 讨论(0)
提交回复
热议问题