我正在尝试以编程方式解析Atom提要.我将原子XML下载为字符串.我可以将XML加载到XmlDocument.但是,我无法使用XPath遍历文档.每当我尝试,我都会null.
我一直在使用这个Atom提要作为测试:http://steve-yegge.blogspot.com/feeds/posts/default
除了我使用" " 时,调用SelectSingleNode()总是返回.这是我现在正在尝试的:null/
using (WebClient wc = new WebClient())
{
string xml = wc.DownloadString("http://steve-yegge.blogspot.com/feeds/posts/default");
XmlNamespaceManager nsMngr = new XmlNamespaceManager(new NameTable());
nsMngr.AddNamespace(string.Empty, "http://www.w3.org/2005/Atom");
nsMngr.AddNamespace("app", "http://purl.org/atom/app#");
XmlDocument atom = new XmlDocument();
atom.LoadXml(xml);
XmlNode node = atom.SelectSingleNode("//entry/link/app:edited", nsMngr);
}
Run Code Online (Sandbox Code Playgroud)
我以为它可能是因为我的XPath,所以我也尝试了一个简单的根节点查询,因为我知道root应该工作:
// I've tried both with & without the nsMngr declared above
XmlNode node = atom.SelectSingleNode("/feed");
Run Code Online (Sandbox Code Playgroud)
无论我做什么,似乎都无法选择任何东西.显然我错过了一些东西,我只是无法弄清楚是什么.为了使XPath能够在这个Atom提要上工作,我需要做些什么?
虽然这个问题有答案,但我发现这个问题几乎完全重复:SelectNodes不能处理stackoverflow feed
在我无法控制的情况下,我有一个XmlDocument,它具有如下结构:
<parent1>
...minor amount of data...
</parent1>
Run Code Online (Sandbox Code Playgroud)
我有另一个XmlDocument,也在我的控制之外,它具有以下结构:
<parent2>
..very large amount of data...
</parent2>
Run Code Online (Sandbox Code Playgroud)
我需要一个XmlDocument格式:
<parent1>
...minor amount of data...
<parent2>
..very large amount of data...
</parent2>
</parent1>
Run Code Online (Sandbox Code Playgroud)
我不想复制parent2.如何在不复制parent2的情况下获得所需的结构?我相信这意味着
oParent1.DocumentElement.AppendChild(oParent1.ImportNode(oParent2.DocumentElement, true));
Run Code Online (Sandbox Code Playgroud)
是不可能的.
有什么好的解决方案吗?
我需要获取XmlNodeList,其中节点名称包含"mystring"
XML
<?xml version="1.0" encoding="utf-8"?>
<root>
<node1>
node1 value
</node1>
<node2_mystring>
node2 value
</node2_mystring>
<node3>
node3 value
</node3>
<node4_mystring>
node 4 value
</node4_mystring>
</root>
Run Code Online (Sandbox Code Playgroud)
期望的输出是
<?xml version="1.0" encoding="utf-8"?>
<root>
<node2_mystring>
node2 value
</node2_mystring>
<node4_mystring>
node 4 value
</node4_mystring>
</root>
Run Code Online (Sandbox Code Playgroud)
我试过类似的东西 XmlNodeList mystringElements = xmlDocument.SelectNodes(@"//*[contains(name,'mystring')]");
但它返回零节点.我应该在XPath查询中放置什么来实现这一目标.
我收到XML字符串中的消息; 我加载进去XmlDocument; 但是第二个节点每次都不同; 我在下面举例说明了三个例子:
<Message>
<Event1 Operation="Amended" Id="88888">Other XML Text</Event1>
</Message>
<Message>
<Event2 _Operation_="Cancelled" Id="9999999"> Other XML Text </Event2>
</Message>
<Message>
<Event3 Operation="Cancelled" Id="22222"> Other XML Text </Event3>
</Message>
Run Code Online (Sandbox Code Playgroud)
现在,我想找出第二个节点是Event1或者Event2还是Event3,也什么操作例如值"修订","取消","有序"?
假设我有一个XmlDocument我生成的InnerXml,看起来像这样:
<ORM_O01>
<MSH>
<MSH.9>
<MSG.2>O01</MSG.2>
</MSH.9>
<MSH.6>
<HD.1>13702</HD.1>
</MSH.6>
</MSH>
<ORM_O01.PATIENT>
<PID>
<PID.18>
<CX.1>SecondTestFin</CX.1>
</PID.18>
<PID.3>
<CX.1>108</CX.1>
</PID.3>
</PID>
</ORM_O01.PATIENT>
</ORM_O01>
Run Code Online (Sandbox Code Playgroud)
如您所见,节点<PID.18>在节点之前<PID.3>.(<MSH.9>也在之前<MSH.6>.)
重构我的一代将导致我干净的代码变得非常混乱.
有没有办法对节点进行排序,以便它排序alpha直到它到达最后一个句点然后排序数字(如果最后的值是数字)?
通过"数字排序"我的意思是它将查看整数而不是char的char.(所以18> 3).
在我的Windows手机应用程序中,我需要使用XmlDocument类.但我仍然会遇到错误
Error 1 The type or namespace name 'XmlDocument' could not be found (are you missing a using directive or an assembly reference?)
我添加了参考和
using System.Xml
但它没有帮助.
这是我的SOAP示例代码,我需要修改它以使用XDocument Edit - 添加SOAP示例代码
public static void CallWebService()
{
var _url = "http://xxxxxxxxx/Service1.asmx";
var _action = "http://xxxxxxxx/Service1.asmx?op=HelloWorld";
XmlDocument soapEnvelopeXml = CreateSoapEnvelope()
HttpWebRequest webRequest = CreateWebRequest(_url, _action);
InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);
// begin async call to web request.
IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);
// suspend this thread until call is complete. You might want to
// …Run Code Online (Sandbox Code Playgroud) 我有一个XMLDocument,我需要读入并转换为一组对象.我有以下物品
public class Location
{
public string Name;
public List<Building> Buildings;
}
public class Building
{
public string Name;
public List<Room> Rooms;
}
Run Code Online (Sandbox Code Playgroud)
我有以下XML文件:
<?xml version="1.0" encoding="utf-8" ?>
<info>
<locations>
<location name="New York">
<Building name="Building1">
<Rooms>
<Room name="Room1">
<Capacity>18</Capacity>
</Room>
<Room name="Room2">
<Capacity>6</Capacity>
</Room>
</Rooms>
</Building>
<Building name="Building2">
<Rooms>
<Room name="RoomA">
<Capacity>18</Capacity>
</Room>
</Rooms>
</Building>
</location>
<location name ="London">
<Building name="Building45">
<Rooms>
<Room name="Room5">
<Capacity>6</Capacity>
</Room>
</Building>
</location>
</locations>
</info>
Run Code Online (Sandbox Code Playgroud)
这样做的最佳方式是什么?我应该自动将xmldocument序列化到对象还是我需要解析每个元素并手动转换为我的对象?特别是,我试图弄清楚如何转换集合(位置,建筑物等).
将此XML文件转换为基本的最佳建议是什么?
List<Location>
Run Code Online (Sandbox Code Playgroud)
对象?
在我被人们认为XML解析器不应该关心元素是空的还是自闭的之前,有一个原因是我不能允许自闭XML元素.原因是我实际上使用的是SGML而不是XML,而我正在使用的SGML DTD非常严格并且不允许它.
我所拥有的是数千个SGML文件,我需要运行XSLT.因此,我必须暂时将SGML转换为XML才能应用XSLT.然后我编写了一个方法,将它们转换回SGML(基本上只是用SGML声明替换XML声明并写回任何其他实体声明,如图形实体).
我的问题是,在转换回SGML之后,当我在SGML编辑器中打开文件时,文件不会解析,因为空元素已经自动关闭.
有没有人知道如何在使用XmlDocument时阻止这种情况发生?
将SGML转换为XML并再次返回的方法如下所示
//converts the SGML file to XML – it’s during this conversion that the
//empty elements get self-closed, i think
private XmlDocument convertToXML(TextReader reader)
{
// setup SgmlReader
Sgml.SgmlReader sgmlReader = new Sgml.SgmlReader();
//sgmlReader.DocType = "HTML";
sgmlReader.WhitespaceHandling = WhitespaceHandling.All;
sgmlReader.CaseFolding = Sgml.CaseFolding.ToLower;
sgmlReader.InputStream = reader;
// create document
XmlDocument doc = new XmlDocument();
doc.PreserveWhitespace = true;
doc.XmlResolver = null;
doc.Load(sgmlReader);
return doc;
}
// method to apply the XSLT stylesheet to the XML document
private …Run Code Online (Sandbox Code Playgroud) 我有一个在 Microsoft Framework 3.5 上创建的 Winform 项目。用户可能安装了Windows 7或Windows XP以及Office 2007或更高版本。
我正在研究一个获取剪贴板数据并将其放入 C# 数据表的过程。我已经创建了一个方法来从剪贴板获取原始数据并将其上传到数据表中。
但在某些情况下,Excel 数据显示一个值,但内部有另一个值:
我正在研究一种从 Excel 获取原始数据的方法:
string XmlFmt = "XML Spreadsheet";
var clipboard = Clipboard.GetDataObject();
if (clipboard.GetDataPresent(XmlFmt))
{
var clipData = clipboard.GetData(XmlFmt);
StreamReader streamReader = new StreamReader((MemoryStream)clipData);
streamReader.BaseStream.SetLength(streamReader.BaseStream.Length - 1);
string xmlText = streamReader.ReadToEnd();
var stream = new StringReader(xmlText);
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xmlText);
DataSet dsExcelData = new DataSet();
dsExcelData.ReadXml(new XmlNodeReader(xmlDocument));
}
Run Code Online (Sandbox Code Playgroud)
但是,此方法会检索一个包含多个表的数据集,其中包含 Excel 数据每个部分的配置:

基本上,我想将这些结构转换为仅包含原始数据的简单数据表。有人可以帮助我提示如何实现这一目标?...我不想在此实现中使用第三方库。