标签: xmldocument

阅读 Stack Overflow RSS 提要

我正在尝试从提要中获取未回答问题的列表,但在阅读时遇到问题。

const string RECENT_QUESTIONS = "https://stackoverflow.com/feeds";

XmlTextReader reader;
XmlDocument doc;

// Load the feed in
reader = new XmlTextReader(RECENT_QUESTIONS);
//reader.MoveToContent();

// Add the feed to the document
doc = new XmlDocument();
doc.Load(reader);

// Get the <feed> element
XmlNodeList feed = doc.GetElementsByTagName("feed");

// Loop through each item under feed and add to entries
IEnumerator ienum = feed.GetEnumerator();
List<XmlNode> entries = new List<XmlNode>();
while (ienum.MoveNext())
{
    XmlNode node = (XmlNode)ienum.Current;
    if (node.Name == "entry")
    {
        entries.Add(node);
    }
}

// Send …
Run Code Online (Sandbox Code Playgroud)

c# xml xmldocument xmlreader feed

2
推荐指数
1
解决办法
4470
查看次数

强制将 XML 字符实体放入 XmlDocument 中

我有一些如下所示的 XML:

<abc x="{"></abc>
Run Code Online (Sandbox Code Playgroud)

我想强制XmlDocument使用括号的XML字符实体,即:

<abc x="&#123;"></abc>
Run Code Online (Sandbox Code Playgroud)

MSDN 是这样说的:

为了分配包含实体引用的属性值,用户必须创建一个 XmlAttribute 节点以及任何 XmlText 和 XmlEntityReference 节点,构建适当的子树并使用 SetAttributeNode 将其分配为属性值。

CreateEntityReference听起来很有希望,所以我尝试了这个:

XmlDocument doc = new XmlDocument();
doc.LoadXml("<abc />");
XmlAttribute x = doc.CreateAttribute("x");
x.AppendChild(doc.CreateEntityReference("#123"));
doc.DocumentElement.Attributes.Append(x);
Run Code Online (Sandbox Code Playgroud)

我得到了例外Cannot create an 'EntityReference' node with a name starting with '#'.

CreateEntityReference 不喜欢“#”的任何原因 - 更重要的是如何将字符实体获取到 XmlDocument 的 XML 中?有可能吗?我希望避免 OuterXml 的字符串操作......

c# xml xmldocument dom

2
推荐指数
1
解决办法
2942
查看次数

为什么XmlDocument.GetElementById总是返回null?

我有一些XML(有效的XHTML),如下所示:

<html>
    <head>
        <script type="text/javascript">
            <![CDATA[
                function change_header(){
                    document.getElementById("myHeader").innerHTML="Nice day!";
                }]]>
        </script>
    </head>
    <body>
        <h1 id="myHeader">Hello World!</h1>
        <button onclick="change_header()">Change text</button>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

我正在尝试#myHeader使用节点,docment.GetElementById("myHeader")但它总是返回null.为什么?

它不会将id属性识别为没有DTD或其他东西 id属性?如果是这种情况,我怎样才能使用HTML DTD?

c# xmldocument sgmlreader

2
推荐指数
1
解决办法
4674
查看次数

XmlDocument中的字符串更大,更少和相等的比较

我正在尝试在XmlDocument中进行字符串比较,以下是我尝试过的.我想知道为什么前2个产生正确的结果,而最后2个没有返回任何结果.

我想要做的是根据日期时间字符串过滤掉节点.就像我的最后一个例子.

谢谢,

XmlNodeList test = x2PathDoc.SelectNodes("//config
                                            /pendingversion
                                              [@versionconfigid > 1002002]");

XmlNodeList test2 = x2PathDoc.SelectNodes("//config
                                             /pendingversion
                                               [@versionconfigid >'1002002']");

XmlNodeList test3 = x2PathDoc.SelectNodes("//config
                                             /pendingversion[@test > 'b']");

XmlNodeList test4 = x2PathDoc.SelectNodes("//config
                                             /pendingversion
                                               [@deploydatetime > 
                                                '2010-12-19T03:25:00-08:00']");
Run Code Online (Sandbox Code Playgroud)

.net c# xpath xmldocument

2
推荐指数
1
解决办法
1530
查看次数

DocumentElement.AppendChild 抛出未设置为对象实例的对象引用

我正在尝试创建一个新的 xml 文件,将数据写入其中然后保存。

这是代码:

XmlDocument doc= new XmlDocument();
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", null, null);
doc.AppendChild(dec);

XmlElement rootnode = doc.CreateElement("Root");

foreach (var item in list)
{
   XmlElement parent = ordersNIA.CreateElement("ParentElement");

   XmlElement childOne = ordersNIA.CreateElement("childOne");
   childOne.InnerText = "This is the first child";
   parent.AppendChild(childOne);

   XmlElement childTwo = ordersNIA.CreateElement("childTwo");
   childOne.InnerText = "This is the second child";
   parent.AppendChild(childTwo);

   XmlElement childThree = ordersNIA.CreateElement("childThree");
   childOne.InnerText = "This is the third child";
   parent.AppendChild(childThree);

   rootnode.AppendChild(parent);

}

doc.DocumentElement.AppendChild(rootnode);
doc.Save("xmlDocument.xml");
Run Code Online (Sandbox Code Playgroud)

该行doc.DocumentElement.AppendChild(rootnode);是抛出

“你调用的对象是空的”

我一直在寻找互联网,但我似乎没有找到为什么会抛出这个错误的答案。

当我检查 rootnode 时,我看到它的 innerHTML 完全充满了我的 xml,所以这似乎是正确的。我没有看到任何空对象,但也许我遗漏了一些东西 …

c# xml xmldocument c#-4.0

2
推荐指数
1
解决办法
4545
查看次数

如何使用数字字符实体而不是问号将 XmlDocument.Save() 编码为“us-ascii”?

我的目标是在不丢失 Unicode 字符的情况下获得 XML的二进制缓冲区(在这种情况下MemoryStream.ToArray()会产生byte[])。我希望 XML 序列化程序使用数字字符引用来表示在 ASCII 中无效的任何内容。到目前为止,我有:

using System;
using System.IO;
using System.Text;
using System.Xml;

class Program
{
    static void Main(string[] args)
    {
        var doc = new XmlDocument();
        doc.LoadXml("<x>“??”</x>");
        using (var buf = new MemoryStream())
        {
            using (var writer = new StreamWriter(buf, Encoding.ASCII))
                doc.Save(writer);
            Console.Write(Encoding.ASCII.GetString(buf.ToArray()));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

上述程序产生以下输出:

$ ./ConsoleApplication2.exe
<?xml version="1.0" encoding="us-ascii"?>
<x>????</x>
Run Code Online (Sandbox Code Playgroud)

我想通了如何告诉XmlDocument.Save()使用encoding="us-ascii"-by移交它TextStreamTextStream.Encoding设置为Encoding.ASCII文档The encoding on the TextWriter determines the …

.net c# xml xmldocument xml-encoding

2
推荐指数
1
解决办法
2362
查看次数

使用冒号 (:) 读取 XML

我正在尝试从以下 XML 文档 ( https://gdata.youtube.com/feeds/api/videos?q=example )获取视频的视图,我能够获取链接和作者,因为标签中没有冒号。

我正在尝试获取 yt:statistics 但我不知道如何获取。

    result = e.Result.Replace("xmlns='http://www.w3.org/2005/Atom' ", String.Empty);

    XmlDocument doc = new XmlDocument();
    doc.LoadXml(result);

    XmlNodeList videos = doc.GetElementsByTagName("entry");

    foreach (XmlNode video in videos)
    {
        XmlNode insideauthor = video.SelectSingleNode("author");

        string videoId = video.SelectSingleNode("id").InnerText.Replace("http://gdata.youtube.com/feeds/api/videos/", String.Empty);
        string author = insideauthor.SelectSingleNode("name").InnerText;

        // Trying to get the views of a video of the search results
        MessageBox.Show(video.SelectSingleNode("yt:statistics").Attributes["viewCount"].InnerText);
    }
Run Code Online (Sandbox Code Playgroud)

c# xmldocument xml-namespaces selectsinglenode

2
推荐指数
1
解决办法
3088
查看次数

在 C# 中替换 xml 元素值

这是我的 xml 文件数据

<Persons>
    <Person>
        <Name>john</Name>
    </Person>
    <Employee>
        <Detail>
            <Firstname>john</FirstName>
        </Detail>
    </Employee>
    <Student>
        <FullName>john</FullName>
    </Student>
</Persons>
Run Code Online (Sandbox Code Playgroud)

我想在所有地方将“john”替换为“danny”。

我怎样才能在 c# 中做到这一点?

c# xml linq-to-entities xmldocument xml-parsing

2
推荐指数
1
解决办法
1万
查看次数

c# XmlDocument SelectNodes 不返回节点

我正在尝试创建一个通用的 XmlParsing 方法。以 Xml 为例:

<body>
<section>
    <subsection1>
        ...
    </subsection1>
    <subsection2>
        ...
    </subsection2>
</section>
<section>
    <subsection1>
        ...
    </subsection1>
    <subsection2>
        ...
    </subsection2>
</section>
</body>
Run Code Online (Sandbox Code Playgroud)

我试图获取所有“部分”节点,但不知道它们有多深或其父节点名称。

到目前为止我已经(我的 XML 是字符串格式)

        XmlDocument xml = new XmlDocument();
        xml.LoadXml(XMLtoRead);

        XmlNodeList nodes = xml.DocumentElement.SelectNodes("//section");
Run Code Online (Sandbox Code Playgroud)

然而,节点计数始终为 0。我的印象是“//”前缀递归地在文档中搜索指定的节点。

我真正的 XML 是 SOAP 回复:

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">


<soap:Body>
    <Response xmlns="http://tempuri.org/">
Run Code Online (Sandbox Code Playgroud)

c# xpath xmldocument selectnodes

2
推荐指数
1
解决办法
2062
查看次数

强制XmlDocument使用显式结束标记保存空元素

我有以下代码:

string PathName = "C:\\Users\\TestUser\\Documents\\Project";
string FileName = "settings.xml";

XmlDocument Settings = new XmlDocument(); 
Settings.Load(Path.Combine(PathName, FileName));

XmlNode KnowledgeNode = Settings.SelectSingleNode("/Config/Map/Knowledge");

XmlNode UsersNode = Settings.CreateNode(XmlNodeType.Element, "Users", null);
XmlAttribute FileDirectory = Settings.CreateAttribute("FileDirectory");
FileDirectory.Value = UserSelectedFileDirectory;
UsersNode.Attributes.Append(FileDirectory);
KnowledgeNode.AppendChild(UsersNode);

Settings.Save(Path.Combine(PathName, FileName));
Run Code Online (Sandbox Code Playgroud)

这导致我的XML文件包含<Users FileDirectory="C:\data" /> 而不是<Users FileDirectory="C:\data" ></Users>我想要的.

如何创建结束元素?我给它一个谷歌,我找不到多少.任何帮助表示赞赏,谢谢.

.net c# xml xmldocument

2
推荐指数
1
解决办法
2711
查看次数