您可以更改XML,以适应使用LINQ的XSD? [英] Can you change an XML to fit an XSD with LINQ?

查看:157
本文介绍了您可以更改XML,以适应使用LINQ的XSD?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要它的重新排列元素的XML。有没有办法做到这一点使用LINQ?



在下面的例子中,元素3和2已经混合起来,重新安排需要使元素1,2和3可能在下一次数据进入时,这将是在偶数顺序不同,但将需要把回1,2和3

 < A> 
将1为卤素;&下/ 1 GT;
将3是氢;&下/ 3 GT;
将; 2 - ;&下; / 2 - ;
< / A>


解决方案

最难的部分是从XSD得到正确的顺序。您可以加载XSD作为的XDocument ,并直接与XML查询。



考虑根据你的榜样,下面的架构(我加了下划线,因为 1 2 ,&安培; 3 不是有效的XML名称本身)

 <?XML版本=1.0编码=UTF-8>?; 
< XS:模式ID =XMLSchema1
的targetNamespace =http://tempuri.org/XMLSchema1.xsd
将elementFormDefault =合格
的xmlns =HTTP ://tempuri.org/XMLSchema1.xsd
的xmlns:mstns =http://tempuri.org/XMLSchema1.xsd
的xmlns:XS =http://www.w3.org / 2001 / XML模式
>

< XS:复杂类型名称=一个>
< XS:序列>
< XS:元素的名称=_ 1类型=XS:字符串的minOccurs =1的maxOccurs =1/>
< XS:元素的名称=_ 2类型=XS:字符串的minOccurs =1的maxOccurs =1/>
< XS:元素的名称=_ 3类型=XS:字符串的minOccurs =1的maxOccurs =1/>
< / XS:序列>
< / XS:复杂类型>
< / XS:架构> `

如果您加载到一个对象本(的XDocument 的XElement 是最好的),你可以做的架构LINQ查询(查询没有经过测试,我从内存从我最后一次这样做这样做,但如果我失去了一些东西,它应该指向你在正确的方向)

  VAR模式= XDocument.Load(@ C:\somepath\XMLSchema1.xsd); 
VAR XS = XNamespace.Get(@http://www.w3.org/2001/XMLSchema);

变种秩序=(从X在schema.Elements(XS +模式)。元素(XS +复杂类型)
其中x.Attributes(名)。FirstOrDefault( ).value的==是
选择x.Element(XS +序)元素())
。首先()
。选择(X =方式> x.Attribute (name)的值);

如果你有一个更复杂的XSD,预先定义的类型和/或自定义名称空间,它将查询得到的元素更复杂的顺序,但希望你的想法。如果你有一个极其复杂的XSD,看到了一些我在这个答案的帮助理解 XmlSchemaInfo 对象模型末尾的链接的,你可能是能者多劳写自己查询能够更轻松地拿到订单。



一旦你有了正确的顺序,我不建议使用LINQ实际更改节点的顺序LINQ是不是旨在修改对象,而是用它来寻找节点和父节点,然后在您想要的顺序重新排列它们。

 的XElement xelem = //得到你需要重新安排

的foreach(以VAR元素)
{
VAR节点= xelem.Element(元素)元素的父元素;
node.Remove();
xelem.Add(节点);
}

如果你要直接在代码中访问XSD频繁地工作,我推荐阅读求救作为 XmlSchemaInfo 的对象模型下面的问题和答案是不是有据可查的,而且需要一定的时间和放大器;努力获得一个可行的方法来提取你所需要的。它会更容易发现和提取你从架构所需要的信息。



的XmlSchema从XML文件推断 - 如何通过在XSD所有元素迭代(莫拉夫斯基的问题,他在他的评论中参考)



Capture架构信息验证时的XDocument(我自己的问题和放大器;答案)



< A HREF =http://stackoverflow.com/q/1595838/250725>在C#中,如何确定一个元素XSD定义的最大长度



http://geekswithblogs.net/.NETonMyMind/archive/2006


/05/02/76957.aspx
(从那里我得到了大部分的信息为我SchemaInfo问题和答案伟大的博客文章)

I have an XML that needs it's elements rearranging. Is there a way to do this using LINQ?

as in the below example, elements 3 and 2 have been mixed up, and need rearranging so the elements are 1,2 and 3. likely the next time data comes in, it will be in an even different order, but will need putting back to 1,2 and 3.

<a>
  <1></1>
  <3></3>
  <2></2>
</a>

解决方案

The hardest part is getting the correct order from the XSD. You can load the XSD as an XDocument and query it directly with XML.

Consider the following schema based on your example (I added the underscores since 1, 2, & 3 are not valid XML names by themselves)

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="XMLSchema1"
    targetNamespace="http://tempuri.org/XMLSchema1.xsd"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/XMLSchema1.xsd"
    xmlns:mstns="http://tempuri.org/XMLSchema1.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>

  <xs:complexType name="a">
    <xs:sequence>
      <xs:element name="_1" type="xs:string" minOccurs="1" maxOccurs="1" />
      <xs:element name="_2" type="xs:string" minOccurs="1" maxOccurs="1" />
      <xs:element name="_3" type="xs:string" minOccurs="1" maxOccurs="1" />
    </xs:sequence>
  </xs:complexType>
</xs:schema>    `

If you load this into an object (XDocument or XElement are best), you can do a LINQ query on the schema (the query is not tested, I am doing this from memory from the last time i did this, but if i am missing something, it should point you in the right direction)

var schema = XDocument.Load(@"C:\somepath\XMLSchema1.xsd");
var xs = XNamespace.Get(@"http://www.w3.org/2001/XMLSchema");

var order = (from x in schema.Elements(xs + "schema").Elements(xs + "complexType") 
             where x.Attributes("name").FirstOrDefault().Value == "a" 
             select x.Element(xs + "sequence").Elements())
            .First()
            .Select(x => x.Attribute("name").Value);

If you have a more complex XSD, with pre-defined types and/or custom name spaces, it will make the query to get the order of the elements more complex, but hopefully you get the idea. If you have an extremely complex XSD, see some of the links i have at the end of this answer for help understanding the XmlSchemaInfo object model and you might be abler to write your own query to get the order far more easily.

Once you have the correct order, I don't recommend using LINQ to actually change the order of the nodes as LINQ was not designed to modify objects, but rather using it to find the nodes and the parent node, then rearranging them in the order you want.

XElement xelem = // get parent element of the elements you need to rearrange

foreach (var element in order)
{
    var node = xelem.Element(element);
    node.Remove();
    xelem.Add(node);                
}

if you are going to be working with accessing XSD directly in code frequently, I recommend reading the following questions and answers for help as the XmlSchemaInfo object model is not well documented, and will take some time & effort to get a workable method to extract what you need. It will be easier finding and extracting the information you need from the schema.

XmlSchema inferred from an XML file - how to iterate through all the elements in the XSD? (Morawski's question he reference in his comment)

Capture Schema Information when validating XDocument (my own question & answer)

In C#, how to determine the XSD-defined MaxLength for an element

http://geekswithblogs.net/.NETonMyMind/archive/2006/05/02/76957.aspx (Great blog posting from where I got most of the info for my SchemaInfo question and answer)

这篇关于您可以更改XML,以适应使用LINQ的XSD?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆