当使用 C# 标记 = "number"时,如何从别名获取"Some text"?


<w:sdtPr>
<w:rPr>
<w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman"/>
<w:kern w:val="2"/>
<w:sz w:val="24"/>
<w:szCs w:val="24"/>
<w:highlight w:val="yellow"/>
<w:lang w:val="uk-UA"/>
</w:rPr>
<w:alias w:val="Some text"/>
<w:tag w:val="number"/>
<w:id w:val="-8449093"/>
<w:placeholder>
<w:docPart w:val="DefaultPlaceholder_-1854013440"/>
</w:placeholder>
<w:text/>
</w:sdtPr>

我使用这部分代码,我必须向用户(Console.WriteLine(显示所有">一些文本";当标签="0"时;数字";

<w:alias w:val="Some text"/>
<w:tag w:val="number"/>

如果有人帮忙,我会很高兴的!

您想要的东西可以使用lineq 巧妙地完成

var doc = XDocument.Load("D:\test.xml");
XNamespace ns = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
var aliases = from stdPr in doc.Descendants(ns + "sdtPr")
where (string)stdPr.Element(ns + "tag").Attribute(ns + "val") == "number"
select (string)stdPr.Element(ns + "alias").Attribute(ns + "val");
foreach(var alias in aliases)
Console.WriteLine(alias);

尝试xml linq:

sing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:temptest.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
List<XElement> sdtPr = doc.Descendants().Where(x => x.Name.LocalName == "sdtPr").ToList();
XNamespace ns = sdtPr.First().GetNamespaceOfPrefix("w");
XElement number = sdtPr.Where(x => (string)x.Element(ns + "tag").Attribute(ns + "val") == "number").FirstOrDefault();
foreach (XElement element in number.Elements())
{
Console.WriteLine("Name : '{0}', Value : '{1}'", element.Name.LocalName, (string)element.Attribute(ns + "val"));
}
Console.ReadLine();
}
}
}

最新更新