给定这个 XML:
<element>Circles & boxes</element>
Run Code Online (Sandbox Code Playgroud)
我想要做的是将元素的字符串值存储为字符串,并将所有字符引用和实体解析为其等效的 unicode 字符。因此,对于这个元素,我想要“圆圈和盒子”。
当我这样做时(文本是代表文本节点的 XText 对象):
string textvalue = text.ToString(SaveOptions.DisableFormatting);
Run Code Online (Sandbox Code Playgroud)
我得到“圆圈和方框”,这不是我想要的。
这可能吗?
我已经给出了元素或属性的本地名称以及文档的架构。确定元素或属性的基本数据类型的最简单方法是什么。我所说的基本数据类型是指 xs:string、xs:date 等(xml 模式的内置数据类型。)
我面临的问题之一是元素类型很少成为基本的内置类型之一。99% 的时间它是一个复杂类型,50% 的时间引用另一个复杂类型,另一个复杂类型引用另一个复杂类型等等。
此模式的一个简单示例:我想找到 Employee/Person/Name/LastName 的基本类型(确定 LastName 是 xs:normalizedString)。在架构 Employee 中定义为 xs:element 和 type="bns:EmployeeType"
EmplyeeType 定义了一个 Person 元素,但它是“PersonType”类型,然后 Name in person 是 NameType,这是一个复杂类型,它扩展了 GeneralName 类型,即 BasicNameType 类型,该类型最终定义了 LastName 类型,其类型为“LastNameType”,然后在。还有定义等。
我目前正在使用 linq-to-xml 编写一个解析器来解决这个问题,但这并不容易或漂亮。我已经搜索了其他解决方案,但没有找到任何解决方案,但我完全承认我对 XML/模式/XPath 的无知。
有没有一种简单的方法来获取元素的基本类型?
我使用以下语法
XDocument config = XDocument.Load(@"path to the xml file");
Run Code Online (Sandbox Code Playgroud)
但是我在 c# 代码中包含此语句。我想让路径可配置,例如在应用程序的 web.config 文件中使用名称路径声明一个键,我应该能够通过 xdocument cofnig 在 c# 代码中获得它=xdocument.Load(路径)。
可以这样吗?
这是我的解决方案的概述:

我已将构建设置为嵌入式资源,并且当我生成应用程序时,XML 文件不会出现在 /Release 文件夹中。这是正确的,我想要这种行为。
现在我尝试将该文件加载到 XDocument 中,以便可以解析其中的数据:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Parsing XML.");
XDocument championXml = XDocument.Load("Champions.xml");
Console.ReadKey();
}
}
Run Code Online (Sandbox Code Playgroud)
我收到文件未找到错误,因为它试图在发布文件夹的完整路径中查找 xml 文件。
如何正确地将这些数据加载到我的 XDocument 中?
我有以下查询:
Dim elementsWithPossibleCCNumbers As IEnumerable(Of XElement) =
xmlTree.Descendants().
Where(Function(element) element.Attributes().
Where(Function(attribute) attribute.Value.Length >= 13
AndAlso attribute.Value.Length <=
16).
Where(Function(attribute) Long.TryParse(attribute.Value, numeric)).Count() = 1).
[Select](Function(x) x)
Run Code Online (Sandbox Code Playgroud)
我最初是使用 Regex 搜索属性,但由于社区中的其他一些人告诉我这是更好的选择,我开始使用 LINQ to XML。我唯一担心的是,对于较大的 XML 字符串,LINQ to XML 查询的性能如何,它是否比 Regex 快?
我可以提高查询速度吗?
我有一个XDocument,我想从中删除XElement。
我试试这个代码:-
XDocument XDoc = XDocument.Parse(XMLFile);
var PricedItineraryRemove = XDoc.Descendants("PricedItinerary");
foreach (XElement xle in PricedItineraryRemove)
{
if (xle.Attribute("SequenceNumber").Value != SequenceNumber.ToString())
{
xEle.Remove(); //this line giving error second time.
}
}
Run Code Online (Sandbox Code Playgroud)
xEle.Remove()第一次正常工作,但第二次给出System.InvalidOperationException异常。
我有一个像这样的 XML 文件:
\n\n我想获取所有标签 a:entry
\n\n<?xml version="1.0" encoding="utf-8"?>\n<a:feed xmlns:a="http://www.w3.org/2005/Atom" xmlns:os="http://a9.com/-/spec/opensearch/1.1/" xmlns="http://schemas.zune.net/catalog/apps/2008/02">\n <a:link rel="next" type="application/atom+xml" href="/v8/catalog/apps?q=facebook&startIndex=50&chunkSize=50&pagingToken=50%7c50&os=8.0.10512.0&cc=US&oc=&lang=vi-VN&hw=469838850&dm=Virtual&oemId=NOKIA&moId=" />\n <a:link rel="self" type="application/atom+xml" href="/v8/catalog/apps?q=facebook&chunkSize=50&os=8.0.10512.0&cc=US&oc=&lang=vi-VN&hw=469838850&dm=Virtual&oemId=NOKIA&moId=" />\n <os:startIndex>0</os:startIndex>\n <os:totalResults>51</os:totalResults>\n <os:itemsPerPage>50</os:itemsPerPage>\n <a:updated>2013-11-01T08:30:11.711450Z</a:updated>\n <a:title type="text">List Of Items</a:title>\n <a:id>tag:catalog.zune.net,2013-11-01:/apps</a:id>\n <pagingToken>50|50</pagingToken>\n <impressionId>cd11c7b0116143dcb4c99f15b72ebbc4</impressionId>\n <a:entry>\n <a:updated>2013-11-01T08:30:11.711450Z</a:updated>\n <a:title type="text">Facebook</a:title>\n <a:id>urn:uuid:82a23635-5bd9-df11-a844-00237de2db9e</a:id>\n <isHardwareCompatible>true</isHardwareCompatible>\n <sortTitle>Facebook</sortTitle>\n <releaseDate>2010-10-19T13:07:17.103000Z</releaseDate>\n <version>5.1.2.0</version>\n <averageUserRating>5.963479</averageUserRating>\n <userRatingCount>42825</userRatingCount>\n <image>\n <id>urn:uuid:f8b42bcd-45c3-4ea5-9c9e-a108ac33cd6e</id>\n </image>\n <hasLiveTile>true</hasLiveTile>\n <categories>\n <category>\n <id>windowsphone.Social</id>\n <title>ma\xcc\xa3ng xa\xcc\x83 h\xc3\xb4\xcc\xa3i</title>\n <isRoot>True</isRoot>\n </category>\n </categories>\n <tags>\n <tag>Independent</tag>\n <tag>LockScreen_Background</tag>\n <tag>LockScreen_Notification_IconCount</tag>\n <tag>LockScreen_Notification_TextField</tag>\n <tag>phone.protocol.fb</tag>\n </tags>\n <offers>\n <offer>\n <offerId>urn:uuid:5c6b4028-74c5-4648-a71e-2ca413a5d2fd</offerId>\n <mediaInstanceId>urn:uuid:64051c69-fb7b-4972-ad42-1dbb2e626a2c</mediaInstanceId>\n <clientTypes>\n <clientType>WindowsPhone80</clientType>\n <clientType>WindowsPhone81</clientType>\n </clientTypes>\n <paymentTypes>\n <paymentType>Credit Card</paymentType>\n <paymentType>Mobile Operator</paymentType>\n </paymentTypes>\n <store>ZEST</store>\n …Run Code Online (Sandbox Code Playgroud) 我有一个 XML 文档(自产),其结构如下:
<?xml version="1.0" encoding="utf-8"?>
<wf:wf version="1.0a" xmlns:wf="http://example.com/workflow" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://example.com/workflow">
<wf:assemblies />
<wf:dataDefinitions />
<wf:processes />
<wf:workflows>
<wf:workflow id="g08615517-cdfd-4091-a053-217a965f7118">
<wf:arguments />
<wf:variables>
<wf:variable id="g39ffecc9-f570-41c1-9ee0-b9358d63da3c" parameterType="Hidden">
<wf:name>UIPTaskId</wf:name>
<wf:dataDefinitionId>gc8f3715c-4a82-42d2-916c-51515083e7e5</wf:dataDefinitionId>
</wf:variable>
<wf:variable id="g46663a0c-7e60-4bd2-80df-16cd544087ad" parameterType="Hidden">
<wf:name>UIPTaskName</wf:name>
<wf:dataDefinitionId>g359FC555-9CC7-47D4-8ED3-EF973E7D74D7</wf:dataDefinitionId>
<wf:value>Responsible Individual</wf:value>
</wf:variable>
<wf:variable id="gb32914d5-6761-4e82-b571-c8944a796fd9" parameterType="Hidden">
<wf:name>Search?</wf:name>
<wf:dataDefinitionId>g57201da8-62b4-46f2-9329-c71d86f39ffc</wf:dataDefinitionId>
<wf:value>True</wf:value>
</wf:variable>
</wf:variables>
</wf:workflow>
</wf:workflows>
</wf:wf>
Run Code Online (Sandbox Code Playgroud)
我有一个实用程序来清理 XML 文档,并使用 XDocument 加载文件,然后循环遍历某些节点并替换值。完成后,我调用 Save 方法将文件保存在新位置,经过进一步检查,Save 方法将删除每个节点上的 wf 前缀。我怎样才能保存这个?难道我做错了什么?这是我的代码示例:
string wf = "wf";
string wkfl = "C:\\MyFiles\\Temp\\myfile1.rrr";
XDocument xdoc = XDocument.Load(wkfl);
XElement variables= xdoc.Descendents(wf + "variables").Single();
foreach(XElement variable in variables.Elements(wf …Run Code Online (Sandbox Code Playgroud) 我有 XML
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
...
Run Code Online (Sandbox Code Playgroud)
我将 xml 加载到 XDocument
XDocument xDoc = XDocument.Parse(xmlString);
Run Code Online (Sandbox Code Playgroud)
接下来我尝试找到XElement包含正文
我试过
XElement bodyElement = xDoc.Descendants(XName.Get("Body", "s")).FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)
或者
XElement bodyElement = xDoc.Descendants("Body").FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)
或者
XElement bodyElement = xDoc.Elements("Body").FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)
但bodyElement始终是null。
如果我尝试添加命名空间
XElement bodyElement = xDoc.Descendants("s:Body").FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)
我有一个关于:.
如果我从 XML 中删除s
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<Body s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
...
Run Code Online (Sandbox Code Playgroud)
一切正常。
如何获得XElement包含Body?
这是xml:
<?xml version="1.0"?>
<model xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" identifier="id-
5f3a493ba7067b8a30bfbf6b"
xmlns="http://www.opengroup.org/xsd/archimate/3.0/">
<name xml:lang="EN"></name>
<views>
<diagrams>
<view identifier="id-4bad55d7-9619-4f21-bcc2-47ddb19d57b3">
<name xml:lang="EN">1shape</name>
<node xmlns:q1="" xsi:type="q1:Shape" identifier="id-97d9fcda-f478-4564-9abd-e2f544d2e292" x="10" y="30" w="1" h="1" nameinternal="BD_shapes_av_Square" shapeId="5bc59d14ec9d2633e8ea102e" angle="0" isgroup="False" alignment="" textalign="0" size="696 360">
<label xml:lang="EN" />
<style>
<fillColor r="102" g="170" b="215" />
<font name="Lato" size="13">
<color r="255" g="255" b="255" />
</font>
</style>
</node>
<node xmlns:q2="" xsi:type="q2:Shape" identifier="id-3b754535-530f-49d2-9e7b-113df0659af9" x="226" y="114" w="1" h="1" nameinternal="BD_shapes_av_Coffee" shapeId="5dad5a884527ecc5c8c4871b" angle="0" isgroup="False" alignment="" textalign="0" size="52.3 72">
<label xml:lang="EN" />
<style>
<fillColor r="102" g="45" b="145" /> …Run Code Online (Sandbox Code Playgroud)